Domanda

I have a database table titled products.

I have 2 columns in the table titled name and image

I have 4 entries in my database

item 1 | image1.jpg
item 2 | image1.jpg
item 3 | image1.jpg
item 4 | image2.jpg

I need to display the results on a page but I only need to display image.1jpg once, my while() loop is obviously displaying all 4 images.

I've been at this for hours, please help!

My code:

$query = mysql_query("SELECT * FROM products WHERE page='Birthdays' ORDER BY id ASC");

while($fetch = mysql_fetch_assoc($query)){  

$image = $fetch['image'];
$name = $fetch['name'];

echo ''.$name.'';
echo '<img src="'.$thumbnail_image.'"/>';   

}
È stato utile?

Soluzione 2

Try the code below;

$query = mysql_query("SELECT * FROM products WHERE page='Birthdays' GROUP BY image ORDER BY id ASC");

while($fetch = mysql_fetch_assoc($query)){  

$image = $fetch['image'];
$name = $fetch['name'];

echo ''.$name.'';
echo '<img src="'.$thumbnail_image.'"/>';   

}

Altri suggerimenti

What you need is using GROUP BY in your query. So change the query as

SELECT * FROM products WHERE page='Birthdays' GROUP BY image ORDER BY id ASC

You can also use a trick like not using while statement at all. If you just write the $fetch = mysql_fetch_assoc($query) without while you would just get the first result in the query. I hope this helps.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top