Re: Q: while loop on an array
| From: | Joe Rice | Date: | Wed, 23 Jan 2002 16:46:04 +0000 |
| Subject: | Re: Q: while loop on an array | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-81708@lists.php.net to get a copy of this message | ||
Thanks to those who pointed me in the right direction.
i couldn't go with a for loop because my keys aren't
sequential, so with a while i came up with this.
$sql = "SELECT aID FROM userAnswers WHERE (";
$num_conditions = sizeof($new_answers);
$count = 1;
reset($new_answers);
while( list ($new_qID,) = each($new_answers)){
$sql .= "qID=\"$new_qID\"";
if ($count < $num_conditions) $sql .= " || ";
$count++;
}
$sql .= ") AND uID = \"$uID\"";
thanks,
joe
Erik Price(pricee@hhbrown.com)@Wed, Jan 23, 2002 at 10:49:24AM -0500:
> 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
> >
>
>
> --
> 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
>