2013-04-23 61 views
1

我的數據庫結構和數據的所有子類ID與PHP是這樣的:選擇類別ID和從MySQL

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時,我有ID爲4,所以我應該選擇4, 5,6。 我知道我應該使用遞歸函數,但我不知道我該怎麼辦呢:-(

+0

http://dipesharea.wordpress.com/2011/06/03/n-level-category/ – 2013-04-23 07:51:10

+0

@DipeshParmar謝謝,但這不是我的工作 – MajAfy 2013-04-23 08:14:48

回答

0

說,如果你是路過的ID在你的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的比你需要嵌套這個循環,以及查詢獲取子類別

+0

謝謝但遞歸函數對我很重要,我想要知道如何使用這個問題的遞歸函數 – MajAfy 2013-04-23 08:13:45

+0

@MajAfy爲什麼你需要一個遞歸函數? – 2013-04-23 08:15:05

-1

給你一些信息,您可以更改colume名稱,並使用

$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; 
+0

謝謝,正如我所說,我需要從數據庫中取數據而不是從數組 – MajAfy 2013-04-23 12:21:40