Fahad Sohail
Fahad Sohail

Reputation: 1836

Capture Next Value for array in foreach

I wan't to capture the next array value and compare it to the current one. I tried doing this with $key+1 in foreach, but other than getting the previous value, its running the current one twice. Can someone help me?

var_dump of the Array

array(5) { [0]=> string(1) "1" [1]=> string(1) "1" [2]=> string(1) "1" [3]=> string(1) "0" [4]=> string(0) "" }

The CODE:

foreach ($q_switch as $key => $value) {

            if ( $q_switch[$key+1] == 0 || empty($q_switch[$key+1]) ) {
                $question_html .= ""
            } else {
                $question_html .=  '<div class="question-wrapper">
                <form method="post">    
                    <div class="wp-postquiz-question">' . $q_question . '</div>
                    <div class="wp-postquiz-question-options">
                        <select name="answer-' . $question_no . '">
                            <option value="A">' .  $q_option_1 . '</option>
                            <option value="B">' .  $q_option_2 . '</option>
                            <option value="C">' .  $q_option_3 . '</option>
                            <option value="D">' .  $q_option_4 . '</option>
                        </select>
                    </div>
                    <input type="hidden" name="question-no" value="' . $question_no . '">
                    <input type="hidden" name="post-id" value="' . $postid . '">
                    <input type="hidden" name="action" value="answer-submitted">
                    <input type="submit" name="submit" class="button primary" value="Answer!">
                </form>
            </div>';
            }
        }

Upvotes: 2

Views: 77

Answers (2)

Fahad Sohail
Fahad Sohail

Reputation: 1836

Thanks for all of the solution's guys. The code which I was trying was working fine but the issue was that I was taking the value as in int turns out the value was stored in string. Also I ended up removing the foreach loop and I just captured the next value by adding 1 in the current question. So basically the whole function looks something like below, in case someone needs it ...

/**
 * Get Question from Database for the post
 * @param  [int] $question_no 
 * @return [string]          
 */
function wp_postquiz_display_question($question_no) {

    // Get the total answered questions by the user.
    do_action('wp_postquiz_total_answered_questions_by_user' );

    // Check if Post Quiz is enabled or disabled.
    global $questions, $answeredQuestions;

    // Create $q_switch array
    $q_switch = array();

    // Get the post id in which quis is being used.
    $postid = get_the_ID();

    // Question Details
    $q_post_content     = $questions['question-' . $question_no . '']['content']; 
    $q_question         = $questions['question-' . $question_no . '']['question'];
    $q_option_1         = $questions['question-' . $question_no . '']['options']['option-1'];
    $q_option_2         = $questions['question-' . $question_no . '']['options']['option-2'];
    $q_option_3         = $questions['question-' . $question_no . '']['options']['option-3'];
    $q_option_4         = $questions['question-' . $question_no . '']['options']['option-4'];

    // HTML to return
    $question_html = '<div class="wp-postquiz-wrapper">
            <div class="wp-postquiz content">' . $q_post_content . '</div>';

    $currnetQuestion = "post-" . $postid . "-question-" . $question_no;

    // If previous question is answered don't include the question for it.
    if ( !in_array($currnetQuestion, $answeredQuestions) ){

        $next = $question_no + 1;
        $q_switch = $questions['question-' . $question_no . '']['q_switch'];
        $q_switch_next = $questions['question-' . $next . '']['q_switch'];

        if ( $q_switch == 1 && !empty($q_switch_next) )  {
                $question_html .=  '<div class="question-wrapper">
                <form method="post">    
                    <div class="wp-postquiz-question">' . $q_question . '</div>
                    <div class="wp-postquiz-question-options">
                        <select name="answer-' . $question_no . '">
                            <option value="" disabled selected>Select the correct answer</option>
                            <option value="A">' .  $q_option_1 . '</option>
                            <option value="B">' .  $q_option_2 . '</option>
                            <option value="C">' .  $q_option_3 . '</option>
                            <option value="D">' .  $q_option_4 . '</option>
                        </select>
                    </div>
                    <input type="hidden" name="question-no" value="' . $question_no . '">
                    <input type="hidden" name="post-id" value="' . $postid . '">
                    <input type="hidden" name="action" value="answer-submitted">
                    <input type="submit" name="submit" class="button primary" value="Answer!">
                </form>
            </div>';                
        }
    }

    $question_html .= '</div>';

    echo $question_html;
}

Upvotes: 1

Narendrasingh Sisodia
Narendrasingh Sisodia

Reputation: 21437

I'm not sure what you're trying to achieve but by looking at your code it seems you want to escape those values that consist of 0 and ''(null/empty) values. So instead of using foreach loop its better to use array_filter function and simply post your rest of code within foreach

$q_switch = array('0'=>"1",'1'=>"1",'2'=>"1",'3'=>"0",'4'=>"");
$result = array_filter($q_switch);
print_r($result);//Array ( [0] => 1 [1] => 1 [2] => 1 )

Upvotes: 1

Related Questions