Forum Moderators: coopster

Message Too Old, No Replies

MySQL loop within a loop?

Is it possible to loop through result set within a loop?

         

Philosopher

9:00 pm on Jun 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hoping someone can save me a bit of time in messing with this.

I am querying a db using the RAND() function and need to have access to the result set in two ways. The first for the full while loop to function properly, but I also need to have a nested loop that goes through the result set entirely, before continuing the next iteration of the first while loop.

Some pseudo code to illustrate.

while($row = mysql_fetch_array($sql)){

do some stuff

Nested while loop (accessing same result set){

some more stuff

}//end of nested while loop

do more stuff

}//end of primary while loop...now back to beginning for next iteration

Basically, run query, begin 1 iteration of while loop accessing result set, then loop through entire result set before starting next iteration of the first while loop and so on.

Exactly how would I accomplish the above without screwing up the db pointer on the primary iteration?

I hope the above make sense...think I confused myself

jatar_k

9:12 pm on Jun 29, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



$outercnt = 0; 
while($row = mysql_fetch_array($sql)){
// do some stuff
mysql_data_seek($sql,0);
while ($internalrow = mysql_fetch_array($sql)){
// some more stuff
}
mysql_data_seek($sql,$outercnt);
// do more stuff
$outercnt++;
}

mysql_data_seek() [ca.php.net] is your friend :)

Philosopher

9:21 pm on Jun 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ahhh...yes, that will definitely help. I knew there had to be something similar to the reset() function but wasn't finding it. That will work perfectly.

Thanks!