Jump to content


Photo

2 queries inside a while loop


  • Please log in to reply
3 replies to this topic

#1 mattennant

mattennant
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 19 April 2006 - 02:00 PM

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

   <?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

any help much appreciated




#2 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 19 April 2006 - 03:23 PM

Try this:

<?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));
}
?>

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.
Think you're smarty?

(Gone until 20 to November)

#3 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 19 April 2006 - 03:26 PM

I think this will do:
   <?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 }
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]
~ D Kuang

#4 mattennant

mattennant
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 19 April 2006 - 05:00 PM

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





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users