Re: Q: while loop on an array

From: Date: Wed, 23 Jan 2002 15:49:24 +0000
Subject: Re: Q: while loop on an array
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-81697@lists.php.net to get a copy of this message
Someone else on the list (cwwesley@udlug.org) passed me an algorithm for something similar last week. I'll try to modify it for your needs: reset($new_answers); $num_conditions = sizeof($new_answers); while (list($new_qID,$new_aID) = each ($new_answers)) {
       $sql .= "qID=\"$new_qID\"";
       for ($c = 0 ; $c < ($num_conditions - 1) ; $c++) {
           $sql .= " || ";
       }
} $sql .= ") AND uID = \"$uID\""; This code is DEFINITELY untested, and probably wrong (I'm new to PHP so I don't have the syntax nailed down yet), but I think you get the idea. As you step through the array in the WHILE loop, you also append " || " to $sql . But you only do this one less than the number of elements in the array $new_answers , so you won't append the final one (which would appear at the very end). Let me know if this works, if only to feed my ego at having been helpful. But I owe cwwesley@udlug.org all the credit, the only thing I did was try to modify it to an array rather than a bunch of static variables (I prefer the static variables). Erik On Wednesday, January 23, 2002, at 10:17 AM, Joe Rice wrote:
Hello, I would like to know how i can know when i'm at the end of an array during a while loop? reset($new_answers); while (list($new_qID,$new_aID) = each ($new_answers)) {
       $sql .= "qID=\"$new_qID\ ||" ";
} $sql .= ") AND uID = \"$uID\""; i would like to know how to stop putting the " || " at the end of $sql in the middle of the while loop. any help would be appreciated. thanks, joe -- 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


« previous php.general (#81697) next »