2017-02-10 80 views
2
類別

子類別我讀取從數據庫的類別和SUBCAT信息如下:腓代碼以顯示下

$cols=array("id","name","description","tags","subcat","img"); 
$result=$conn->get("category",null,$cols);` 

注意,結果就像$result[0]['name']一個多維陣列將顯示類別名稱和$result[0]['subcat']將顯示第一類別和子類別。 下面是摘錄的休息:

$subcat="Notset"; 
$cat=array(); 
$sub=array(); 
$j=0; 
for ($i=0;$i<$conn->count; $i++) 
{ 
    if($result[$i]['subcat'] != $subcat) 
    { 
     $subcat=$result[$i]['subcat']; 
     $sub[$j]=$subcat; 
     $j++; 
    } 
$cat[$i]=$result[$i]['name']; 
} 

但是我的得到的結果如下: Snapshot of web page

enter image description here

但我想下下筆記本電腦類臺式機類和MacBook顯示索尼請在上圖中紅色按鈕是綠色的子類別。

+0

我無法理解你的代碼。它在做什麼? –

+1

請在'$ result = $ conn-> get(「category」,null,$ cols)行之後發佈'var_dump($ result);'的結果'' – jetblack

+0

您只檢查'$ result [$我] ['subcat']'不是'Notset'。你實際上並沒有檢查它是哪個父母。 –

回答

0

您可以使用下面的代碼:(您需要設置$fetched_categories = $result在你的例子。)

function get_subcategories($array, $category_name){ 
    $return = array(); 

    foreach ($array as $key => $category){ 
     if ($category['subcat'] == $category_name){ 
      $category['sub_categories'] = get_subcategories($array, $category['name']);    
      $return[] = $category; 
     } 
    } 

    return $return; 
} 

function print_categories($array, $level = 0){ 
    echo ($level > 0 ? '<ol>' : '<ul>'); 

    foreach ($array as $category){ 
     echo '<li>' . $category['name'] . '</li>'; 
     print_categories($category['sub_categories'], ++$level); 
    } 

    echo ($level > 0 ? '</ol>' : '</ul>'); 
} 

$fetched_categories = array(); 
$fetched_categories[] = ['name' => 'Camera', 'subcat' => '']; 
$fetched_categories[] = ['name' => 'Macbook', 'subcat' => 'Laptops']; 
$fetched_categories[] = ['name' => 'Laptops', 'subcat' => '']; 
$fetched_categories[] = ['name' => 'Desktops', 'subcat' => '']; 
$fetched_categories[] = ['name' => 'Sony', 'subcat' => 'Desktops']; 
$fetched_categories[] = ['name' => 'DSLR', 'subcat' => 'Camera']; 

$categories = get_subcategories($fetched_categories, ''); 

print_categories($categories); 

這裏是輸出:

https://eval.in/734955

0

我完成此使用下面的PHP代碼:

# categories with a parent id of 0 are parent categories 
$selectparents = $db->prepare(" 
SELECT category_id, category_name 
FROM categories 
WHERE parent_category_id = 0 
ORDER BY category_name 
"); 

$selectparents->execute(); 

while($row = $selectparents->fetch(PDO::FETCH_ASSOC)){ 
     $p_category_id = $row['category_id']; 
     $category_name = $row['category_name']; 

     echo '<b>'.$category_name.'</b><br>'; 

      # get sub categories for this parent 
      $selectchilds = $db->prepare(" 
      SELECT category_id, category_name 
      FROM categories 
      WHERE parent_category_id = ".$p_category_id." 
      ORDER BY category_name 
      "); 
      $selectchilds->execute(); 

      while($row = $selectchilds->fetch(PDO::FETCH_ASSOC)){ 
      $category_id = $row['category_id']; 
      $category_name = $row['category_name']; 

      echo '<b>'.$category_name.'</b><br>'; 

      } 
} 

基本上所有父類已經一個PARENT_ID設置爲0,其被查詢以字母順序,然後爲每個潛艇的while循環中查詢爲父母和在父項下按字母順序呈現。