2016-04-15 67 views
5

下面是我的表結構:顯示分類表

菜單表

id title position 
--------------------  
1 Test home 
2 Test2 home 

類別

cid name parent parent_menu 
-------------------------------- 
1 ABC  0   1 
2 DEF  0   2 
3 GHI  1   0 
4 JKL  2   0 

類別說明

id cat_id catdesc slug 
------------------------------- 
1  1  ABC_DESC abc 
2  2  DEF_DESC def 
3  3  GHI_DESC ghi 
4  4  JKL_DESC jkl  
  • Menu table處理菜單標題的位置。
  • Category table處理類別名稱和其他參數。 (如果parent = 0,則表示這是主要類別等等。)
  • Category Description table處理描述,slug和其他參數。

現在我要像下面

Name  Description  Edit   Delete  /*table headings*/ 
------------------------------------------------------- 
    Menu Title: (Test) Main Category Name: (ABC)  
------------------------------------------------------ 
GHI  GHI_DESC  edit_icon  delete_icon 
______________________________________________________ 
    Menu Title: (Test2) Main Category Name: (DEF) 
------------------------------------------------------ 
    JKL  JKL_DESC  edit_icon  delete_icon 

我試圖在PHP,但沒有運氣使用連接和操作數據顯示的數據。

SELECT * FROM `category` t1 LEFT JOIN `category_description` t2 ON t1.cid = t2.cat_id WHERE 1 

然後在PHP中我試圖像下面

<?php $i = 1; foreach($subcat as $sub) { ?> 
    <?php if($sub->parent == 0) { ?> 
     <tr><td><?php echo $sub->name ?></td></tr> 
    <?php } ?> 
    <?php if($sub->parent != 0) { ?> 
     <tr><td><?php echo $sub->name ?></td><td><?php echo $sub->catdesc ?></td> 
     <td>Edit</td><td>Delete</td></tr> 
    <?php } ?> 
<?php } ?> 

而且上面印像表如下:

Main Category Name: ABC 
Main Category Name: DEF 
------ 
GHI GHI_DESC 
JKl JKL_DESC 

請建議如何打印達到目標。

回答

1

假設parent_menu是從菜單表ID,試試這個:

SELECT t1.title, t2.name, t2.parent, t2.parent_menu, t3.catdesc 
FROM menu t1 
LEFT JOIN category t2 ON t1.id=t2.parent_menu 
LEFT JOIN description t3 ON t2.cid=t3.cat_id 
GROUP BY t2.name 

sql demo和​​

+0

您的查詢不返回GHI和JKL名稱和說明有關他們..我說,只有對GHI和JKL描述應reurned – Gags

+1

我調整您的查詢,現在我得到想做的事情:) ..謝謝 – Gags

+0

這是創建問題,當您有相同的t2.names :( – Gags

0

您可以使用舊版本的MySQL方法:

select mt.title, cat.name, cdesc.catdesc from menu_title mt, category cat, category_description cdesc where mt.id = cat.parent_menu and cat.cid = cdesc.cat_id 
+0

但是這是我在做什麼..您選擇特定列 – Gags

+0

一次檢查,我編輯 –

+0

一樣沒有通過Mawia ..所以沒有點 – Gags

0

我會推薦給你在ArrayIterator和RecursiveIteratorIterator的幫助下構建樹並遍歷樹。
要建立你的樹,你可以調整這個代碼示例:

$tree = array(); 
foreach($result_set as $result) { 
    if ($result["parent"] == 0) { 
     $tree["cat"][$result["cid"]]["parent"] = $result; 
    } else { 
     $tree["cat"][$result["parent"]]["child"][$result["cid"] = $result; 
    } 
} 

記住:你必須調整它!
然後你就可以實現迭代器,像這樣:

class CategorieTreeIterator extends ArrayIterator implements RecursiveIterator 
{ 
    public function getChildren() 
    { 
     $link_data = $this->current(); 
     $cid  = key($link_data["cat"]); 
     return new CategorieTreeIterator($link_data["cat"][$cid]["child"]); 
    } 

    public function hasChildren() 
    { 
     $link_data = $this->current(); 
     $cid  = key($link_data["cat"]); 
     return !empty($link_data["cat"][$cid]["child"]); 
    } 
} 

同樣,你必須調整它。
要顯示的數據,你現在可以遍歷使用:

$cat_tree_iter = new CategorieTreeIterator($tree); 
$rec_iter_iter = new RecursiveIteratorIterator($cat_tree_iter, RecursiveIteratorIterator::SELF_FIRST); 

foreach($rec_iter_iter as $iter) { 
    //display data with help of $iter->getDepth(); 

} 
+0

這是複雜的買賣,我的情況;) – Gags

+0

我知道看起來很複雜,但它確實不是。:-)你只需要創建一個類和樹,但不應該對上述代碼的提示太難了......此外,如果將來要添加子類別,將會很容易實現。但是,如果它適用於現在也很好的SQL。 :) – Shimu

+0

是的..如果我沒有錯,那麼它應該被包括在MVC中的助手類? – Gags