Jump to content

Archived

This topic is now archived and is closed to further replies.

mattennant

2 queries inside a while loop

Recommended Posts

hello there

i'm not sure if this is dead simple or impossible. I'm trying to retrieve one set of query results from a database if one condition is met, otherwise i want to display another set of query results. to loop these results i am using a while loop. This is working for one condition, but i don't know how to make it work for the other condition. Sorry i don't know if this is the right terminology. Here's the code

[code]   <?php do { ?>
        <tr>
            <td valign="top"><img src="
            
            <?php
            if  ($colname_services_body == "-1") {
            echo $row_top_image['image'];
            }else{
            echo $row_image['image'];
            }
            ?>
            
            " width="197" /></td>
        </tr>
        
        <?php } while ($row_image = mysql_fetch_assoc($image));

                //after this i'm not sure tried things such as

or while ($row_top_image = mysql_fetch_assoc($image)); ?>

// but that ain't working[/code]

any help much appreciated


Share this post


Link to post
Share on other sites
Try this:

[code]<?php
$i = 0;
do { ?>
        <tr>
            <td valign="top"><img src="
            
            <?php
            if  ($colname_services_body == "-1") {
            echo $row_top_image['image'];
            $i++;
            }else{
            echo $row_image['image'];
            $i++;
            }
            ?>
            
            " width="197" /></td>
        </tr>
        
<?php } while ($row_image = mysql_fetch_assoc($image));

if($i == 1)
{
while ($row_top_image = mysql_fetch_assoc($image));
}
?>
[/code]

What basicly it does is setting a var called 'i' that its value is 0. In the do...while loop each time $i is being increased. If the loop runs just once (in case that the condition of the while is false), $i's value will be 1. If the loop runs more than once, its will be greater than 1. So I added the second while this way if($i == 1){second while};

Hope it helps :)
Orio.

Share this post


Link to post
Share on other sites
I think this will do:
[code]   <?php do { ?>
        <tr>
            <td valign="top"><img src="

            <?php
            if ($colname_services_body == "-1") {
               while ($row_image = mysql_fetch_assoc($image)) {
                  echo $row_image['image'];
               }
            } else {
               mysql_data_seek($image, 0);
               while ($row_top_image = mysql_fetch_assoc($image)) {
                  echo $row_top_image['image'];
               }
            }
            ?>

            " width="197" /></td>
        </tr>

        <?php } [/code]
What I don't understand though, is why you are doing this since $row_top_image['image'] and $row_image['image'] are exactly the same thing (will have the same values, because its the same result resource "$image").

Also note that since you are fetching the same result more than once, I had to move the internal pointer back:

mysql_data_seek():
[a href=\"http://www.php.net/manual/en/function.mysql-data-seek.php\" target=\"_blank\"]http://www.php.net/manual/en/function.mysql-data-seek.php[/a]

Share this post


Link to post
Share on other sites
thanks so much for your help

i think i may be seriously over complicating this business

in essence what i am trying to do is display 3 images (tagged firstpage) from a database when the user enters the page.

ie. when the user hits www.domain.php

the variable $colname_services_body is "-1"

therefore display thes images

echo $row_top_image['image']; selecting the first page images from the database

if the user then goes to www.domain.php?RecordID=myvariable

then three images tagged with myvariable are displayed

i'm guesing that i'm making a simple thing complicated, but it sure is doing my brain in and i'm not really sure if that makes it clear, thanks again mat

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.