以树结构加载数据


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]: https://i.stack.imgur.com/1oW5j.png
  [2]: https://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>";
                        }
                    }
                }
            }
嘿,

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

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);

最新更新