4

I tested looping nested While statements so:

$count1 = 0;

while ($count1 < 3) {
 $count1++;
 $count2 = 0;
 echo "count1: ".$count1."<br />";

    while ($count2 < 3) {
    $count2++;
    echo "count2: ".$count2."<br />";
    }
}

This works perfectly (looping three times each) with results:

count1: 1
 count2: 1
 count2: 2
 count2: 3
count1: 2
 count2: 1
 count2: 2
 count2: 3
count1: 3
 count2: 1
 count2: 2
 count2: 3

Then I tried the same with a loop using mysql_fetch_assoc ($ContactsInterests is a two row associative array, and $LatestNews has 50 rows) i.e.

$CI_count = 0;

while ($CI_Row = mysql_fetch_assoc($ContactsInterests)) { //loop thru interests
$CI_count++;
$LN_count = 0;
echo "CI_count: ".$CI_count."<br />";

while ($LN_Row = mysql_fetch_assoc($LatestNews)) { //loop thru news
    $LN_count++;
    echo "LN_count: ".$LN_count."<br />";

}
}

The results are:

CI_count: 1
 LN_count: 1
 LN_count: 2
 ...
 LN_count: 50
 LN_count: 51
CI_count: 2

But where it the second iteration of LN_count? I don't understand why the LN_count didn't increment a second time.

Help appreciated.

5
  • at the top add $LN_Row = array(); Then, at the end of the first while loop add reset($LN_Row); Commented Jun 23, 2012 at 19:41
  • @redskins80 the $LN_Row contains only the last value passed by mysql_fetch_assoc... so it won't fill up the $LN_Row unless you do something like this $LN_Row[] = mysql_fetch_assoc Commented Jun 23, 2012 at 19:49
  • @Dexter youre right! ive deleted my answer. sry. Commented Jun 23, 2012 at 19:51
  • 1
    STOP USING THE mySQL_* EXTENSIONS IMMEDIATELY. They are being deprecated. Use either PDO our the I extensions. Commented Jun 23, 2012 at 19:51
  • Don't worry guys, @Jeremy is probably sleeping by now, he's not responding. Commented Jun 23, 2012 at 19:58

4 Answers 4

5

mysql_fetch_assoc does iteration for "mysql result" type. Seeks index for each fetch. you must use mysql_data_seek to go to the first result like;

<?php

    $CI_count = 0;

    while ($CI_Row = mysql_fetch_assoc($ContactsInterests)) { //loop thru interests
        $CI_count++;
        $LN_count = 0;
        echo "CI_count: ".$CI_count."<br />";

        mysql_data_seek($LatestNews,0);
        while ($LN_Row = mysql_fetch_assoc($LatestNews)) { //loop thru news
            $LN_count++;
            echo "LN_count: ".$LN_count."<br />";

        }
    }
Sign up to request clarification or add additional context in comments.

Comments

0

Because the results have been exhausted. You've iterated through all of them... If you wanted to loop again you're have to repopulate the $LatestNews variable.

Comments

0

You need to reset the mysql internal pointer:

see http://php.net/mysql_data_seek

Comments

0

mysql_fetch_assoc() takes out one by one the rows of the source, you when you will be in the second step of the first loop, you wont have any rows in the source variable.

You need to put the results of the second query in an array, then loop the array, and not using mysql_fetch_assoc.

Comments

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.