2016-04-15 29 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