1
I want sort out the data from Categories table like tree structure as given below…

类别表在该表上有带有他的父 ID 的类别......而且每个类别可能有很多孩子......而且每个孩子可能有很多内在孩子......我想根据那个父母得到所有孩子通过动态…… 我无法获取超出某个级别的数据(动态方式)......如何分别获得所有类别和他的孩子的树结构......这可能吗?

![TREE STRUCTURE][1]


From the Table 'categories' as given below...

!['CATEGORIES' TABLE IMAGE][2]


  [1]: http://i.stack.imgur.com/1oW5j.png
  [2]: http://i.stack.imgur.com/dJ7ht.png



my static code is

$list_pcat_q = mysql_query("select * from categories where parent_id='0'");
            while($list_pcat_f = mysql_fetch_array($list_pcat_q)){
            $parent=extract($list_pcat_f);
            echo"<h3><a href='categories_list.php?cate_id=$cat_id'>".$cat_name."</a></h3>";

                $list_scat_q = mysql_query("select * from categories where parent_id=$cat_id");
                while($list_scat_f = mysql_fetch_array($list_scat_q)){
                extract($list_scat_f);
                echo "<h4><a href='categories_list.php?cate_id=$cat_id'>".$cat_name."</a></h4>";

                    $list_sscat_q = mysql_query("select * from categories where parent_id=$cat_id");
                    while($list_sscat_f = mysql_fetch_array($list_sscat_q)){
                    extract($list_sscat_f);

                        echo "<h5><a href='categories_list.php?cate_id=$cat_id'>".$cat_name."</a></h5>";

                        $list_ssscat_q = mysql_query("select * from categories where parent_id=$cat_id and final_flag='1'");
                        while($list_ssscat_f = mysql_fetch_array($list_ssscat_q)){
                        extract($list_ssscat_f);

                        echo "<h6><a href='categories_list.php?cate_id=$cat_id'>".$cat_name."</a></h6>";

                        }

                    }

                }

            }
4

1 回答 1

1

嘿试试这个,让我知道它是否有效。

function childCount($parent_id){

$query=mysql_query("select * from categories where parent_id=".$parent_id);
return mysql_num_rows($query);
}
function getCategories($parent_id,$output='')
{
$output='<ul>';
$sql="select * from categories where cat_id!='' ".$parent_id;
$query=mysql_query($sql);
if(mysql_num_rows($query)>0)
{
    while($category=mysql_fetch_array($query))
    {
        if(childCount($category['cat_id'])>0)
        {
            $output.='<li>'.$category['cat_name'];
            $t=" and parent_id=".$category['cat_id'];
            $output.=getCategories($t);
            $output.='</li>';
        }
        else
        $output.='<li>'.$category['cat_name'].'</li>';
    }
}
$output.='</ul>';
return $output;

}
$t=" and parent_id=0";
echo getCategories($t);
于 2013-11-11T05:38:36.847 回答