Re: while loop
| From: | Marco Roccioletti | Date: | Wed, 08 Nov 2000 15:27:28 +0000 |
| Subject: | Re: while loop | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-24330@lists.php.net to get a copy of this message | ||
On Wed, 08 Nov 2000, Ali wrote:
> Hello all,
>
> I have got a while loop which retrieve data from a DB. Within this while
> loop I have got another while loop which retrieve its data from the same DB
> if the if statement is fulfilled. My piece of code looks:
>
>
> <?
> if ($action == "alle") {
> $sql = "SELECT * FROM Rubriken ORDER BY rub_id";
> $q -> query ($DB, $sql);
> while ($row = $q ->getrow()) {
> echo $row['rubrik'];
> if ($rubrik == $row['run_name']) {
> $sql = "SELECT * FROM $rubrik ORDER BY id";
> $q -> query ($DB, $sql);
> $num = $q -> numrows();
> if ($num > 0) {
> while ($row = $q ->getrow()) {
> echo $row['unterrubrik'];
> }
> }
> }
> }
> }
> ?>
>
> Now my problem is that when $rubrik does not exists the first while loop
> works fine. But when $rubrik exists the first while loop stops after doing
> the second loop for the first time. Does anyone know why the first loop
> behaves like this and not going through. I appreciate any help. Thank you.
>
>
> Ali
>
It's probably because you use in both loops $q as your db object, erasing the
result you need for the outer loop inside the inner loop: After the first pass
in the inner loop, $q contains the query of the inner loop (unterrubrik) the
result pointer is at the end of the result set due to the inner
while ($row = $q->getrow())
So on the second run of the outer loop, $q->get_row() returns false, because
there is no more result.
Try using self-explanatory var names, and not "$q" or "$i"!
Cheers,
Marco