Re: help - WHILE in FOREACH

From: Date: Wed, 08 Jan 2003 06:30:00 +0000
Subject: Re: help - WHILE in FOREACH
Groups: php.general 
Request: Send a blank email to php-general+get-130713@lists.php.net to get a copy of this message
My appologies for the lack of explicit information. Thank you for taking the time to write a reply. That is exactly what I needed to know. Thanks again, James
From: Chris Wesley <cwwesley@udlug.org> To: php-general@lists.php.net Subject: Re: [PHP] help - WHILE in FOREACH Date: Tue, 7 Jan 2003 18:49:53 -0800 (PST) On Tue, 7 Jan 2003, James Brennan wrote: The while statement is only executing correctly for the first run through the foreach loop. Why? It's /real/ hard to tell when you don't give any context or otherwise explain what it is you want to achieve. Since the code in fact executes correctly, we can only explain what's going on in your code, not why it doesn't work for your needs ... You while() loop runs through a mysql result set in its entirety the first time through the foreach() loop and leaves the result pointer at the end of the result set. All subsequent runs of the while() loop will start fetching results from the end of $show_names ... and get /nothing/. If you want to use all the results out of $show_names again, you have to reset the result pointer to the beginning of $show_names. Use mysql_data_seek(). http://php.net/mysql_data_seek, and see below. foreach($desc as $key=>$value) { printf("Show %s Description <select name='show%sDesc'>", $key, $key); echo "<option value = ''></option>"; while($row = mysql_fetch_row($show_names)) {
    printf("<option value='%s'>%s</option>", $row[0], $row[1]);
}
    if( mysql_num_rows( $show_names ) > 0 ){
      mysql_data_seek( $show_names, 0 );
    }
echo "</select><br>"; } g.luck, ~Chris -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
_________________________________________________________________ STOP MORE SPAM with the new MSN 8 and get 2 months FREE* http://join.msn.com/?page=features/junkmail

« previous php.general (#130713) next »