[Solved] Looping through 2 tables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

[Solved] Looping through 2 tables

Post by Addos »

Hi,
I have a blank trying to figure out how to do the following. I have 2 tables tbl_products tbl_prdtimages and I need to run a loop through both of these to get a thumb nail image and the product_ID to pass through the URL to my results page. I can get most of this working however the image that I’m getting is the first from the database which means the loop is only being applied to the tbl_products table and ignoring the tbl_prdtimages table. I have a working example here and you can see the same image listed throughout however once each item is clicked the correct id is passed to the results page. So can anyone give me a few pointers as to how I can loop through both tables.

http://www.batchelorscoffeecompany.ie/b ... mages3.php

This is the code from the page.
Many thanks

Code: Select all

<?php
mysql_select_db($database_*****, $*******);
$query_GetImages = "SELECT * FROM tbl_products,tbl_prdtimages
WHERE tbl_prdtimages.prdctImage_ImgTypeID = '1'
GROUP BY tbl_products.product_ID";
$GetImages = mysql_query($query_GetImages, $b******) or die(mysql_error());
$row_GetImages = mysql_fetch_assoc($GetImages);
$totalRows_GetImages = mysql_num_rows($GetImages);

?>

<?php do {

<a href="details.php?prodId=<?php echo $row_GetImages['product_ID']; ?>"><img src="cw3/assets/product_thumb/<?php echo $row_GetImages['prdctImage_FileName']; ?>"  alt="<?php echo $row_GetImages['product_ShortDescription']; ?>" border="0" > <br><?php echo $row_GetImages['product_ShortDescription']; ?></a><br><br>
<?php }  while ($row_GetImages = mysql_fetch_assoc($GetImages)); ?>
Last edited by Addos on Sun Jul 08, 2007 4:33 am, edited 1 time in total.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

You need to define a relationship between the two tables in order to join them.
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Post by Addos »

Thanks for the reply.
Can you give me a example of how I define a relationship as I thought I already had but I'm still on a learning curve and must be missing the most obvious.
Thanks again
B
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Post by Addos »

Ahh ok I've just sorted that. Dang! so obvious anyway thanks for the pointer it sorted me out well.
:wink:
Post Reply