Page 1 of 1

Multiple queries in loop

Posted: Mon Jun 07, 2010 6:27 pm
by bookwalla
Hi,

I am quering mysql database to get the name of an image, and then attaching the name of the image to a list tag. this way I can poplulate the list with images dynamically:
<?php
$query = "SELECT filename from files";
$result = db_query($query);

print "<ul>";
{
while($row=db_fetch_array($result))
{
$filename = $row['filename'];


print "<li><a href=\"/sites/mysite.com/files/$filename\" class=\"01\"><img src=\"/sites/mysite.com/files/$filename\" /></a></li>\n";
}
}
print "</ul>\n";
?>

The above code works fine, but I also want to add an image description inside this <li>:
print "<li><h1>$description</h1><a href=\"/sites/mysite.com/files/$filename\" class=\"01\"><img src=\"/sites/mysite.com/files/$filename\" /></a></li>\n";

So each image in a list item will also have a description.
The problem is that the description of the image is in a completely different table in the database: SELECT description from upload

So how do I make this happen for the description part. the filename php is working good.

Re: Multiple queries in loop

Posted: Mon Jun 07, 2010 6:32 pm
by califdon
You Join the tables in the query, assuming that you have a foreign key in the descriptions table that identifies which primary key it refers to. It should be a one-to-one relationship. In case there might be some images for which there is no description in the other table, you would want to make it an Inner Join so that you get ALL the records from the image table, with matching descriptions if they exist.