【问题标题】:Show Category table with parent categories显示包含父类别的类别表
【发布时间】:2016-04-15 06:26:13
【问题描述】:

下面是我的表结构:

菜单表

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 中使用 JOINS 和操作数据,但没有成功。

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

请根据需要建议如何打印。

【问题讨论】:

    标签: php mysql mysqli


    【解决方案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 demophp demo

    【讨论】:

    • 您的查询没有返回 GHI 和 JKL 的名称和关于它们的描述。我说只有 GHI 和 JKL 的描述应该重新返回
    • 我调整了您的查询,现在我得到了想要的东西 :) .. 谢谢
    • 当你有相同的 t2.names 时这会产生问题:(
    • 我已经用演示更新了我的答案。连接中的问题是由于 parent_menu 字段中的零 (0) 数据造成的。
    • 这很好,但数据没有像我提到的那样显示。 :(
    【解决方案2】:

    你可以使用旧的 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 
    

    【讨论】:

    • 但这就是我正在做的......你选择了特定的列
    【解决方案3】:

    我会建议您构建一棵树,然后在 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();
    
    }
    

    【讨论】:

    • 在我的情况下这很复杂;)
    • 我知道它看起来很复杂,但实际上并非如此。 :-) 你只需要创建一个类和树,上面代码中的提示应该不会太难......另外,如果你将来添加子类别,它会很容易实现。但是,如果它适用于 SQL,那么现在也可以。 :)
    • 是的。如果我没记错的话,它应该作为辅助类包含在 MVC 中??
    • 是的,您可以轻松地重复使用它
    • 但我的问题是它应该是新类还是我应该扩展现有的助手类(坏主意;)
    猜你喜欢
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-09
    • 1970-01-01
    • 2021-06-15
    • 1970-01-01
    相关资源
    最近更新 更多