Re: Resetting the pointer in an array used for a while() loop
| From: | Torsten Roehr | Date: | Mon, 03 May 2004 11:39:20 +0000 |
| Subject: | Re: Resetting the pointer in an array used for a while() loop | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-185183@lists.php.net to get a copy of this message | ||
"Dave G" <ml@autotelic.com> wrote in message
news:000501c430f6$b8c3bc20$0b01a8c0@green...
> PHP Listers,
> In my code, I call for some data from MySQL and use the returned
> results to drive a while() loop, as in the following code:
>
> <?php
> $queryA = "SELECT row1, row2 FROM table WHERE row1=" . $variable;
> $resultA = mysql_query($queryA);
> while($array = mysql_fetch_array($resultA))
> {
> file://Do stuff
> }
> ?>
>
> The problem is that later on in the code, I want to do another
> while() loop, using the same results from $QueryA. But having already
> been used in a while loop already, the results of
> mysql_fetch_array($ResultA) seem to be at the end of the list.
> So I want to set the pointer back to the start, but using
> reset() doesn't seem to have an effect. I assume this is because the
> while loop is not cycling through the contents of $array, but through
> the contents of $resultA. However, $resultA is not actually an array,
> but the source data which gets made into an array by
> mysql_fetch_array(). So, saying reset($resultA) returns an error.
>
> How can I use the results of mysql_fetch_array($resultA) for a second
> while() loop?
Maybe you could copy the result set and then use the copy for your second
loop.
If this doesn't work maybe you could do a workaround with this:
$secondArray = array();
while($array = mysql_fetch_array($resultA))
{
// do your stuff
// add to secondArray for second loop
$secondArray[] = $array;
}
Regards, Torsten
>
> I hope my question is clear. Any advice would be much appreciated. Thank
> you.
>
> --
> Yoroshiku!
> Dave G
> ml@autotelic.com
>
>