Page 1 of 1

[Solved] Looping through 2 tables

Posted: Sat Jul 07, 2007 6:03 pm
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)); ?>

Posted: Sat Jul 07, 2007 6:20 pm
by superdezign
You need to define a relationship between the two tables in order to join them.

Posted: Sun Jul 08, 2007 4:26 am
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

Posted: Sun Jul 08, 2007 4:32 am
by Addos
Ahh ok I've just sorted that. Dang! so obvious anyway thanks for the pointer it sorted me out well.
:wink: