Re: Weird nested while loop problem

From: Date: Wed, 17 Jan 2001 22:15:22 +0000
Subject: Re: Weird nested while loop problem
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-35180@lists.php.net to get a copy of this message
Thanks! This is starting to make sense...although when I try to reset what I thought was my array, I get the "Variable passed to reset() is not an array or object" error message. Here's the relevant code: $relationsquery = "select * from relations where childtype='$childtype' and parenttype='$parenttype' and parentsku='$parentsku'"; $relationsresult = mysql_query($relationsquery) or die(mysql_error()); while ($relationsrow = mysql_fetch_array($relationsresult)) { $thischild = $relationsrow["childsku"]; if ($thevalue == $thischild) { echo " checked"; } } // end while for relations loop reset($relationsresult); Thanks again for your help so far! Maurice At 2:00 PM -0800 1/17/01, Rasmus Lerdorf wrote:
What I'm getting is that the inner while loop executes on the first execution of the outer while, and then not at all. How would I get it to execute each time?
Coming from a perl background, I got stuck there too when I started using PHP. PHP has this odd notion of an array pointer. After the first time you loop through the array, the pointer is at the end, so the second loop won't really do anything. Use the reset frunction to reset the array pointer to the first element.
The array pointer can be pretty useful. ie. give me the second last element of an array: end($arr); echo prev($arr); Or have it tell you what the index is at the second last element: end($arr); prev($arr); echo key($arr); and the index before that one: prev($arr); echo key($arr); Perl has these too, they just aren't user accessible. -Rasmus -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net For additional commands, e-mail: php-general-help@lists.php.net To contact the list administrators, e-mail: php-list-admin@lists.php.net
-- Maurice Rickard http://mauricerickard.com/

« previous php.general (#35180) next »