Jump to content

While() is display 30 of the same items..i only need it to display once


jeger003

Recommended Posts

i finally figured out how to JOIN two tables and now the it goes through but it displays the same item 30 times

 

like:

 

car

car

car

car

car

house

house

house

house

house

 

it would display car 30 times and house 30 times

 

what am i doing wrong?

 

here is my code

<?php

$id = 'listings.id';
$query_image = mysql_query("SELECT listings_urls.listings_id,listings_urls.thumb_url,listings.title,listings.image FROM listings_urls,listings WHERE listings_urls.listings_id = $id AND listings.live = 1") or die(mysql_error());

$num = mysql_num_rows($query_image);   

if($num > 0) 
   {
    while ($fetch = mysql_fetch_array($query_image) )
      {

if($fetch['image']== 1)

{

echo "<img src='/".$fetch['thumb_url']."'><br>";

echo "<br>".$fetch['title'];	

}	

else	

{
echo "no image to display";	
}


      }
   }

else
   {                                              
      echo "no items found!";
   }
?>

You created a cartesian join. The code below should fix that.

 

"SELECT lu.listings_id,lu.thumb_url,li.title,li.image FROM listings_urls lu,listings li WHERE lu.listings_id = li.listings_id AND lu.listings_id = $id AND listings.live = 1"

 

The above is assuming both have a field called "listings_id" that you can relate the two tables together.

Archived

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

×
×
  • Create New...

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.