如何仅查询第二级别类别?

时间:2011-06-23 10:31:31

标签: php mysql

这是我的代码,它显示所有类别,如

  

一个
  -A.1
  -A.2
    乙
  -B.1
  -B.2

     

现在我希望它只显示

     
    

A.1
        A.2
        B.1
        B.2

  

有什么想法吗?

    function current_categories($m,$id){
    $cat_array=array();
    $sql = "select * from `category`";
    $query = DB::Query($sql);
        while($row = DB::fetch_array($query)){
            $cat_array[] = array($row['id'],$row['name'],$row['classid'],$row['sort']);
        } 
        if($id==""){
            $id=0;
        }
    $n = str_pad('',$m,'-',STR_PAD_LEFT);
    $n = str_replace("-","    ",$n);
    for($i=0;$i<count($cat_array);$i++){
        if($cat_array[$i][2]==$id){
        echo "<li>"."<a href=\"/team/index.php?gid=".$cat_array[$i][0]."\">".$cat_array[$i][1]."</a></li>"; 
    current_categories($m+2,$cat_array[$i][0]);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我不知道你为toplevel classid值作为classid放入了什么值。可能是NULL还是-1?

function second_level_categories(){
    $sql = " SELECT lvl2.* FROM `category` as lvl1 "
          ." JOIN `category` as lvl2"
          ."     ON lvl1.id = lvl2.parent_id"
          ." WHERE lvl1.parent_id=VALUE_FOR_TOP_LEVEL"
          ." ORDER BY lvl1.sort ASC, lvl2.sort ASC"

    $result = DB::Query($sql);
    while($category = DB::fetch_array($result)){
        echo '<li><a href="/team/index.php?gid='.$category['id'].'">"';
        echo $category['name'];
        echo '</a></li>';
    } 
}