I have a site with lots of images organized in categories and subcategories. Whenever a category is chosen (say, "Pets"), I want to display the list of all available subcategories so that each subcategory has a title and 3 sample images, like that:
Dogs
image image image
Cats
image image image
Rats
image image image
The tables are structured as follows:
img_id subcat_title img_title sample
---------------------------------------------------------------
1 dogs someimage 1
2 cats another_image 0
3 rats yet_another_image 1
4 rats someimage2 1
5 dogs another_image3 0
5 cats someimage6 1
The query couldn't have been simpler:
$samples = mysql_query("SELECT subcat_title, img_title FROM table WHERE sample = '1'") or die(mysql_error());
while ($sample = mysql_fetch_array($samples))
{
echo $sample['subcat_title'] . '<br /><img src="/pets/' . $sample['img_title'] . '.jpg" />';
}
That's where I'm stuck: the above code gives me either 1) one subcategory title and one image or 2) several images and the subcategory titles attached to each and every image. Is there a way to display the subcategory title just once? I'd appreciate any fresh ideas...