- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
你好
我有一个关于类别和子类别的问题
我有这样的表:
ID----- 姓名---- ParentID
1 ------ A ---------- 0
2 ------ B ---------- 0
3 ------ C ---------- 1
我已经显示 A B C ok 。但我需要它会像这样显示:
一个
-- C
B
只是有问题,找不到这样做的方法(在父类别下创建子类别)
注意:我在 Views 中使用 make query 成功了,但我认为它不太好。
我想找到更好的方法
非常感谢
最佳答案
迁移以下类别表:
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->tinyInteger('parent_id')->default(0);
$table->timestamps();
});
输入您的类别和子类别,如下表结构:
+-------------+----------------------+----------+
| id | name |parent_id |
+-------------+----------------------+----------+
| 1 | ELECTRONICS | 0 |
| 2 | TELEVISIONS | 1 |
| 3 | TUBE | 2 |
| 4 | LCD | 2 |
| 5 | PLASMA | 2 |
| 6 | PORTABLE ELECTRONICS | 1 |
| 7 | MP3 PLAYERS | 6 |
| 8 | FLASH | 7 |
| 9 | CD PLAYERS | 6 |
| 10 | 2 WAY RADIOS | 6 |
+-------------+----------------------+----------+
MySQL层次递归查询
$categories = DB::table('categories as c1')
->leftJoin('categories as c2','c2.parent_id','=','c1.id')
->leftJoin('categories as c3','c3.parent_id','=','c2.id')
->leftJoin('categories as c4','c4.parent_id','=','c3.id')
->select('c1.id as c1_id','c1.name as c1_name','c2.id as c2_id','c2.name as c2_name','c3.id as c3_id','c3.name as c3_name','c4.id as c4_id','c4.name as c4_name')
->where('c1.parent_id','=',0)
->get();
输出:
+-----------------+--------------------------+------------------+-----------------+
|c1_id| c1_name | c2_id| c2_name | c3_id| c3_name | c4_id| c4_name |
+-----------------+--------------------------+------------------+-----------------+
| 1 | ELECTRONICS | 2 | TELEVISIONS | 3 | TUBE | NULL |
| 1 | ELECTRONICS | 2 | TELEVISIONS | 4 | LCD | NULL |
| 1 | ELECTRONICS | 2 | TELEVISIONS | 5 | PLASMA | NULL |
| 1 | ELECTRONICS | 6 | PORTABLE ELECTRONICS | 7 | MP3 PLAYERS | 8 | FLASH |
| 1 | ELECTRONICS | 6 | PORTABLE ELECTRONICS | 9 | CD PLAYERS | NULL |
| 1 | ELECTRONICS | 6 | PORTABLE ELECTRONICS | 10| 2 WAY RADIOS | NULL |
+-------------+--------------------------------------+--------------+-------+
然后,将$categories
值放入数组
。
$all_categories = [];
$category_c1_id = '';
$category_c2_id = '';
$category_c3_id = '';
$category_c4_id = '';
$c1_id = -1;
$c2_id = -1;
$c3_id = -1;
$c4_id = -1;
foreach ($categories as $k=>$category){
if($category->c1_id != $category_c1_id){
$c1_id = $c1_id +1;
$all_categories[$c1_id]['id'] = $category->c1_id;
$all_categories[$c1_id]['name'] = $category->c1_name;
$c2_id = -1;
}
if($category->c2_id != $category_c2_id && $category->c2_id <> NULL){
$c2_id = $c2_id +1;
$all_categories[$c1_id]['children'][$c2_id]['id'] = $category->c2_id;
$all_categories[$c1_id]['children'][$c2_id]['name'] = $category->c2_name;
$c3_id = -1;
}
if($category->c3_id != $category_c3_id && $category->c3_id <> NULL){
$c3_id = $c3_id +1;
$all_categories[$c1_id]['children'][$c2_id]['children'][$c3_id]['id'] = $category->c3_id;
$all_categories[$c1_id]['children'][$c2_id]['children'][$c3_id]['name'] = $category->c3_name;
$c4_id = -1;
}
if($category->c4_id != $category_c4_id && $category->c3_id <> NULL){
$c4_id = $c4_id +1;
$all_categories[$c1_id]['children'][$c2_id]['children'][$c3_id]['children'][$c4_id]['id'] = $category->c4_id;
$all_categories[$c1_id]['children'][$c2_id]['children'][$c3_id]['children'][$c4_id]['name'] = $category->c4_name;
}
$category_c1_id = $category->c1_id;
$category_c2_id = $category->c2_id;
$category_c3_id = $category->c3_id;
$category_c4_id = $category->c4_id;
}
return $all_categories;
输出:
$categories = [
['id' => 1, 'name' => 'TV & Home Theather'],
['id' => 2, 'name' => 'Tablets & E-Readers'],
['id' => 3, 'name' => 'Computers', 'children' => [
['id' => 4, 'name' => 'Laptops', 'children' => [
['id' => 5, 'name' => 'PC Laptops'],
['id' => 6, 'name' => 'Macbooks (Air/Pro)']
]],
['id' => 7, 'name' => 'Desktops'],
['id' => 8, 'name' => 'Monitors']
]],
['id' => 9, 'name' => 'Cell Phones']
];
现在显示在选择产品上传选项。
<div class="form-group">
<label>Select Categories</label><br/>
@foreach($categories as $category)
@php($category = (object)$category)
<div class="form-check">
<label class="form-check-label" for="{{$category->id}}">
{{ Form::checkbox('category_ids[]', $category->id, null, array('id'=>$category->id)) }}
{{$category->name}}
</label>
@if(isset($category->children))
@foreach($category->children as $subcategory)
@php($subcategory = (object)$subcategory)
<div class="form-check" style="margin-left: 15px;">
<label class="form-check-label" for="{{$subcategory->id}}">
{{ Form::checkbox('category_ids[]', $subcategory->id, null, array('id'=>$subcategory->id)) }}
{{$subcategory->name}}
</label>
@if(isset($subcategory->children))
@foreach($subcategory->children as $subsubcategory)
@php($subsubcategory = (object)$subsubcategory)
<div class="form-check" style="margin-left: 15px;">
<label class="form-check-label" for="{{$subsubcategory->id}}">
{{ Form::checkbox('category_ids[]', $subsubcategory->id, null, array('id'=>$subsubcategory->id)) }}
{{$subsubcategory->name}}
</label>
</div>
@endforeach
@endif
</div>
@endforeach
@endif
</div>
@endforeach
</div>
开启菜单选项
<ul class="nav primary clone-main-menu toggle-slide-menu" id="mercado_main" data-menuname="Main menu" >
@foreach($categories as $category)
<li class="menu-item menu-item-has-children item-megamenu" >
<a href="#" title="Digital & Electronics" class="link-term mercado-item-title">{{$category['name']}}</a>
<div class="wrap-megamenu">
<div class="megamenu-content min-height-510 bg-vertical-digital">
@if(isset($category['children']))
<div class="row">
@foreach($category['children'] as $subcategory)
<div class="col-lg-3 col-md-3">
<div class="wrap-vertical-nav">
<h2 class="menu-title"><a href="#" class="link-term">{{$subcategory['name']}}</a></h2>
@if(isset($subcategory['children']))
<ul>
@foreach($subcategory['children'] as $subcategory)
<li class="menu-item"><a href="#" class="link-term">{{$subcategory['name']}}</a></li>
@endforeach
</ul>
@endif
</div>
</div>
@endforeach
</div>
@endif
</div>
</div>
</li>
@endforeach
</ul>
关于Laravel - 子类别显示在主要类别下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24257892/
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 9 个月前关闭。 Improve
我使用 partykit打包并遇到以下错误消息: Error in matrix(0, nrow = mi, ncol = nl) : invalid 'nrow' value (too large
我一直在尝试寻找一个量表或分类指标,为 VADER 情绪分析分配一些情感程度,而不仅仅是积极、消极或中性。如果有人可以分享他们的观点或资源来帮助按以下方式对 VADER 复合分数进行分类,我将非常感激
伙计们,我想自动循环..但我不知道是我放错了 while 还是循环错了? 我的数据库 标签:kt_barang kd_kategori | nama_kategori 1
我正在创建一个列出本地企业并按类别、子类别和关键字对它们进行分组的应用程序。以下是企业排序规则: 一个企业可以属于多个类别和子类别 一个企业可以有多个关键字 并非每个类别都有子类别,但有子类别的只有两
我有一系列单词 - 我的刺激 - 它们显示在屏幕上。然而,每个词都有另一个“条件”,即它们是类别 A、类别 B 或类别 C。这可能很简单,但我找不到答案并坚持下去。我的最终目标是在每次运行脚本时将类别
我正在使用 Laravel 5.5 和 MySql。如果不向 Services 表中添加 subCategoryID 列,我无法弄清楚如何将类别和子类别与服务相关联。 目前这是我的表结构 服务类别 i
我有两个部分/类别结构的链接表。 the section table structure id sec_title 1 section 1 2 section 2 the category str
我有一个类层次结构如下 @interface PTLDatasource : NSObject ... @interface PTLFetchedDatasource : PTLDatasource
我有一个 DataFrame df 一列,category 使用以下代码创建: import pandas as pd import random as rand from string import
我经常在多个类中设置获取请求以从核心数据(加上一些其他结果)中检索“allRecipes”或“lastModifiedDate”。 为此使用专门的类别 NSManagedObjectContext+R
可以在 Objective C 中创建类别之间的依赖关系吗?也在类别和它们的基类之间? 我知道在运行时应该没有区别,它们可能只是在编译时合并在一起。例如,假设我将 B 类分解为: B(base cla
这个问题在这里已经有了答案: 关闭 10 年前。
example img of a category selection by user 嘿,我正在尝试设置一个选择,用户必须选择一个类别和第二个类别,但我不知道如何获取他单击的信息。用户单击类别后,它
尝试将投资组合库添加到我正在制作的自定义 wp 主题中。我已经筋疲力尽地试图寻找甚至可以修改一些的解决方案和插件。我认为我在寻找解决方案时遇到的一个问题是我不完全确定哪些搜索词可以帮助我找到与我想要实
当我查看 Cocoa Touch API 时,我可以在同一个头文件中找到一些与类别一起声明的类,例如 @interface NSArray : NSObject @property (readonl
我的 log4j.properties 中有以下内容 log4j.rootLogger = debug, stdout, fileLog log4j.appender.stdout = org.apa
如果我在类中添加类别方法,比如NSXMLNode: @interface NSXMLNode (mycat) - (void)myFunc; @end NSXMLNode 的子类,例如 NSXMLEl
先说场景,wordpress的分类结构是这样的 Level 1: Top Level 2: -Nextme_1 Level 3: --Nextme_2 --Nextme_3 Leve
我有一个解析网络,现在我想浏览标签,或显示图表。我怎样才能得到图表?或者在树中导航。显示第一步然后其他等。并了解这棵树是如何 build 的。 import urllib from lxml impo
我是一名优秀的程序员,十分优秀!