php显示类别和子类别

时间:2011-03-13 18:44:32

标签: php mysql while-loop do-while

好了,我现在有两张表categoriessubcategories这样的

Categories:
  id
  title
  description

Sub Categories:
  id
  catid
  title
  description

现在,子类别的“catid”将具有“id”类别的值,我想将我的类别和子类别分组并显示,如

Category
  subcat
  subcat

categories
  subcat
  subcat

1 个答案:

答案 0 :(得分:1)

$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 />");
     }
}

这应该显示您的类别和子类别:

  

类别:Category1(这是   category1)
子类别:SubCategory1   这是子类别1子类别:   SubCategory2这是子类别2   类别:Category2(这是   category2)
子类别:子类别3   这是子类别3

等等