Keshav Nair Keshav Nair - 16 days ago 5
MySQL Question

php display categories and subcategories

Ok now i have two table

categories
and
subcategories
like this

Categories:
id
title
description

Sub Categories:
id
catid
title
description


Now the '
catid
' of the subcategories will have the value of categories '
id
' and i want to group my and loop my categories and sub categories and display it like

Category
subcat
subcat

categories
subcat
subcat

Answer
$catquery = "select * from categories"; 
$res = mysql_query($catquery)

while (list($id, $title, $description) = mysql_fetch_row($res)){
     $subquery = "select id, title, description from subcategories where catid = {$id}";
     $res = mysql_query($subquery);
     echo("Category: {title} ({description})<br />");
     while (list($sid, $stitle, $sdescription) = mysql_fetch_row($res)){
          echo (" Subcategory: {stitle} {sdescription}<br />");
     }
}

This should display your categories and subcategories as such:

Category: Category1 (This is category1)
Subcategory: SubCategory1 This is subcategory1
Subcategory: SubCategory2 This is subcategory2
Category: Category2 (This is category2)
Subcategory: Subcategory3 This is subcategory3

and so on