1

我的数据库结构和数据是这样的:

id    |   sub   |   title
------+---------+----------
1     |   0     | Cat 1
2     |   0     | Cat 2
3     |   1     | Cat1-1
4     |   2     | Cat2-1
5     |   2     | Cat2-2
6     |   5     | Cat2-2-1

现在,当我有 id 4 时,我想选择所有子类别 ID,所以我应该选择 4,5,6 。我知道我应该使用递归函数,但我不知道该怎么做:-(

4

2 回答 2

0

假设您在 URL 中传递 id

所以你可以像

$category_id = $_GET['id']; //Say 4 here, 
//Also don''t forget to validate that ID and sannitize it before you use it in your query

so now you can fetch the sub categories like

$fetch_sub_cat = mysqli_query($connection, "SELECT * FROM table_name WHERE id = $category_id");

while($throw_results = mysqli_fetch_array($fetch_sub_cat)) {
   echo $throw_results['id'];
}

如果您正在循环您的类别 ID,那么您需要嵌套此循环以及查询以获取子类别

于 2013-04-23T07:51:21.623 回答
-1

给你一些信息,你可以更改列名并使用

$data=array(
array('Menu_ID'=>1, 'Menu_Name'=>'Catalog', 'Menu_Link'=>'#', 'Menu_ParentID'=>0),
array('Menu_ID'=>2, 'Menu_Name'=>'Reports', 'Menu_Link'=>'#', 'Menu_ParentID'=>0),
array('Menu_ID'=>3, 'Menu_Name'=>'Products','Menu_Link'=> '#','Menu_ParentID'=> 1),
array('Menu_ID'=>4, 'Menu_Name'=>'Sales','Menu_Link'=> '#', 'Menu_ParentID'=>2),
array('Menu_ID'=>5, 'Menu_Name'=>'Customers','Menu_Link'=> '#', 'Menu_ParentID'=>2),
array('Menu_ID'=>6, 'Menu_Name'=>'Tvs','Menu_Link'=> '#','Menu_ParentID'=> 3));

print_r(loop_menu($data));

// Menu_ID Menu_Name Menu_Link Menu_ParentID
function loop_menu($rows,$parent = 0){
 $arr=array();
 $i=0;
  foreach ($rows as $row)
  { 
        if (array_key_exists('Menu_ParentID',$row) && $row['Menu_ParentID'] == $parent){

                if(array_key_exists($i,$arr)){
                    $arr[$i]=array();
                }
                $arr[$i]['data']=$row;
                $arr[$i]['child']= loop_menu($rows,$row['Menu_ID']);
                $i++;
        }
  }
    return $arr;
}

然后

Array
(
    [0] => Array
        (
            [data] => Array
                (
                    [Menu_ID] => 1
                    [Menu_Name] => Catalog
                    [Menu_Link] => #
                    [Menu_ParentID] => 0
                )

            [child] => Array
                (
                    [0] => Array
                        (
                            [data] => Array
                                (
                                    [Menu_ID] => 3
                                    [Menu_Name] => Products
                                    [Menu_Link] => #
                                    [Menu_ParentID] => 1
                                )

                            [child] => Array
                                (
                                    [0] => Array
                                        (
                                            [data] => Array
                                                (
                                                    [Menu_ID] => 6
                                                    [Menu_Name] => Tvs
                                                    [Menu_Link] => #
                                                    [Menu_ParentID] => 3
                                                )

                                            [child] => Array
                                                (
                                                )

                                        )

                                )

                        )

                )

        )

    [1] => Array
        (
            [data] => Array
                (
                    [Menu_ID] => 2
                    [Menu_Name] => Reports
                    [Menu_Link] => #
                    [Menu_ParentID] => 0
                )

            [child] => Array
                (
                    [0] => Array
                        (
                            [data] => Array
                                (
                                    [Menu_ID] => 4
                                    [Menu_Name] => Sales
                                    [Menu_Link] => #
                                    [Menu_ParentID] => 2
                                )

                            [child] => Array
                                (
                                )

                        )

                    [1] => Array
                        (
                            [data] => Array
                                (
                                    [Menu_ID] => 5
                                    [Menu_Name] => Customers
                                    [Menu_Link] => #
                                    [Menu_ParentID] => 2
                                )

                            [child] => Array
                                (
                                )

                        )

                )

        )

)

然后将数组之类的代码编码为 ul

http://sandbox.onlinephpfunctions.com/code/2b3ab04f959413ebf75b65034edd60da61ed0020

另一种数组样式

$arr[$i]['data'] = $row;
$arr[$i]['child']= loop_menu($rows,$row['Menu_ID']);

改成

$row['child'] = loop_menu($rows,$row['Menu_ID']);
$arr[$i] = $row;
于 2013-04-23T08:40:48.917 回答