1

I have a while and foreach loop that I want to use to print all of my results from a MySQL query. Below is my code for the while statement and the foreach statements.

WHILE:

while ($row_questions = mysql_fetch_array($result_questions)) {

 $step_number[]      = $row_questions['step_number'];

 if ($row_questions['step_number_sub'] != 0) {
   $step_number_sub[]  = $row_questions['step_number_sub'];
 } else {
   $step_number_sub[]  = "0";
 };

 if ($row_questions['step_required'] != 0) {
   $step_required[]  = "*";
 } else {
   $step_required[]  = "";
 };

 $step_description[] = $row_questions['step_description'];
 $step_outcome[]     = $row_questions['step_outcome'];
 $step_equipment[]   = $row_questions['step_equipment'];
 $test_changes[]     = $row_questions['test_changes'];
};

and my FOREACH:

foreach ($step_number as $i => $step){
    $even_odd = ( '-odd' != $even_odd ) ? '-odd' : '';
    echo '<section class="zebra'.$even_odd.'">';
    echo '<div class="span-2 number"><strong>'.$step_number[$i].'</strong></div>';
    echo '<div class="span-20 description">'.$step_description[$i].'</div>';
    echo '<div class="span-2">'.$step_required[$i].'</div>';
    if ($step_outcome[$i] != null) {
      echo '<div class="span-22 outcome"><strong>Desired Outcome</strong><br>'.$step_outcome[$i].'</div>';
    }
    echo '<div class="clear"></div>';
    echo '<article class="results">';
    echo '<div class="span-10">Did this step match the desired outcome?</div>';
    echo '<div class="span-10">Notes:</div>';
    echo '<div class="span-10">
          <select name="question_'.$step[$i].'" id="question_'.$step[$i].'" required aria-required="true">
          <option name="pass" value="Pass">Yes, this step was completed successfully</option>
          <option name="fail" value="Fail">No, this step failed to complete. See notes below</option>
          </select>
          </div>';
    echo '<div class="span-10"><textarea name="question_'.$step[$i].'" id="question_'.$step[$i].'" class="nots"></textarea></div>';
    echo '</article>';
    echo '<div class="clear"></div>';
    echo '</section>';
};

I am getting multiple versions of the same results (4). Thanks in advance for any help and let me know if you need any other info!

3
  • Whats the output of print_r($step_number) between these two loops? Commented Jan 17, 2012 at 23:10
  • 3
    Have you checked whether your query is creating the duplicates (by using phpmyadmin, for example?) Commented Jan 17, 2012 at 23:29
  • Masterful! It was my SQL. I am a dolt. Thanks so much Aerik! Commented Jan 17, 2012 at 23:55

1 Answer 1

1

Don't use multiple arrays. Use a class structure or array for each element. A simple way to do this:

while ($row_questions = mysql_fetch_array($result_questions))
    questions[] = $row_questions;

foreach( $questions as $question )
{
    $even_odd = ( '-odd' != $even_odd ) ? '-odd' : '';
    echo '<section class="zebra'.$even_odd.'">';
    echo '<div class="span-2 number"><strong>'.$question['step_number'].'</strong></div>';
    echo '<div class="span-20 description">'.$question['step_description'].'</div>';
    echo '<div class="span-2">'.$question['step_required'].'</div>';
    if ($question['step_outcome'] != null) 
    {
      echo '<div class="span-22 outcome"><strong>Desired Outcome</strong><br>'.$question['step_outcome'].'</div>';
    }
   ... you get the drift ...
  };
Sign up to request clarification or add additional context in comments.

1 Comment

oh awesome! Even though the problem was in my SQL, this is a far better solution than what I had! Thanks so much -

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.