简体   繁体   English

如何使用 php 创建菜单树?

[英]How to create a menu tree using php?

I am developing one e-commerce platform where admin can manage menu, category and sub-category.我正在开发一个电子商务平台,管理员可以在其中管理菜单、类别和子类别。 Now, I want display it in mega menu.现在,我想在大型菜单中显示它。

Menu1
    Category1.1
        SubCategory1
        SubCategory2
    Category1.2
        SubCategory
Menu2
    Category2.1
        SubCategory1

This is a possible structure with I came up.这是我想出的一个可能的结构。 I have 2 db tables - Menu, Category.我有 2 个数据库表 - 菜单、类别。 In category, there is a column called sub_category where I am storing sub category values with , separator.在类别中,有一个名为sub_category的列,我在其中使用,分隔符存储子类别值。

Now, after running below query -现在,在运行以下查询后 -

$data = DB::select('SELECT m.id AS mId, m.name AS menu, 
m.meta_keywords AS menu_keywords, m.meta_description AS menu_description, 
c.id AS cId, c.name AS category, c.sub_category, c.meta_keywords AS
category_keywords, c.meta_description AS category_description 
FROM `menu` AS m LEFT JOIN `category` AS c ON c.menuId = m.id 
WHERE m.is_publish = 1 AND c.is_publish = 1');

I am getting result.我得到了结果。 点击

Now, I want to build a tree where I can get a result something like this -现在,我想建立一棵树,我可以得到这样的结果 -

"id" => 1,
"name" => "Fashion"
"category" => [
    1 => [
        "cId" => 1,
        "category" => "Men"
        "sub_category" => "Shirt, T-shirt"
    ],
    2 => [
        "cId" => 2,
        "category" => "Women"
        "sub_category" => ""
    ],
] 

Please help me out to find out solution.请帮助我找出解决方案。 Thank you in advance.先感谢您。

As per your db structure and requirement, you can use below function -根据您的数据库结构和要求,您可以使用以下 function -

public function menuTree($data){
        $menuId = $menu = $sub = $op = array();
        foreach($data as $d){
            if(in_array($d->mId, $menuId)){
                $sub['cId'] = $d->cId;
                $sub['category'] = $d->category;
                $sub['sub_category'] = $d->sub_category;
                $sub['category_keywords'] = $d->category_keywords;
                $sub['category_description'] = $d->category_description;
                $menu[$d->mId]['category'][] = $sub;
            } else {
                $menuId[] = $d->mId;
                $op['id'] = $d->mId;
                $op['menu'] = $d->menu;
                $op['menu_keywords'] = $d->menu_keywords;
                $op['menu_description'] = $d->menu_description;
                $menu[$d->mId] = $op;
                if($d->cId != NULL){
                    $sub['cId'] = $d->cId;
                    $sub['category'] = $d->category;
                    $sub['sub_category'] = $d->sub_category;
                    $sub['category_keywords'] = $d->category_keywords;
                    $sub['category_description'] = $d->category_description;
                    $menu[$d->mId]['category'][] = $sub;
                }
            }
        }
        return $menu;
    }

Still I believe there will be a better solution.我仍然相信会有更好的解决方案。 Hope this works for you.希望这对你有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM