作者:陈业贵 华为云享专家 51cto(专家博主 明日之星 TOP红人)
前言
和大家共同完成获取多级分类数据
一、什么是多级分类数据?
就是很多很多的数据,按照无限极分类结构排序。每一个数组的所有数据都是顶级分类及其其下数据
二、使用步骤
$allcate=$this->order('listorder asc')->field('id,parent_id,cate_name')->select()->toArray();
第一步:这张表qing_category。把这张表所有的数据转换成数组格式。取出字段id,parent_id,cate_name。按照从小到大排序。
foreach ($allcate as $k => $v) {
if($v['parent_id']==0){
第二步:遍历数组并取出顶级分类的数据,放进$data[]=$v;
这里。
foreach ($allcate as $k1 => $v1) {
if($v1['parent_id']==$v['id']){
第三步: 取出某个顶级分类其下的二级分类的所有数据。方进$v['children'][]=$v1;
中。
foreach ($allcate as $k2 => $v2) {
if($v2['parent_id']==$v1['id']){
$v1['children'][]=$v2;
}
第四步:取三级分类的所有数据放进$v1['children'][]=$v2;
中
最后返回 return $data;
核心:是一个顶级分类三层一个顶级分类三层的来的哦,直到全部遍历完毕。
sql
代码如下(示例):
-- phpMyAdmin SQL Dump
-- version 4.8.5
-- https://www.phpmyadmin.net/
--
-- 主机: localhost
-- 生成日期: 2022-06-04 19:25:49
-- 服务器版本: 5.7.26
-- PHP 版本: 7.3.4
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- 数据库: `kkk`
--
-- --------------------------------------------------------
--
-- 表的结构 `qing_category`
--
CREATE TABLE `qing_category` (
`id` int(10) UNSIGNED NOT NULL COMMENT '栏目id',
`cate_name` varchar(30) NOT NULL COMMENT '栏目名称',
`seo_title` varchar(150) NOT NULL COMMENT '栏目标题',
`seo_keywords` varchar(150) NOT NULL COMMENT '关键词',
`seo_description` varchar(255) NOT NULL COMMENT '描述',
`content` text COMMENT '内容',
`status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '状态 1:显示 0:隐藏',
`thumb` varchar(150) DEFAULT NULL COMMENT '图片',
`link` varchar(150) NOT NULL COMMENT '栏目外链',
`listorder` smallint(6) NOT NULL DEFAULT '50' COMMENT '排序',
`parent_id` int(11) NOT NULL DEFAULT '0' COMMENT '上级id',
`type_id` int(30) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
--
-- 转存表中的数据 `qing_category`
--
INSERT INTO `qing_category` (`id`, `cate_name`, `seo_title`, `seo_keywords`, `seo_description`, `content`, `status`, `thumb`, `link`, `listorder`, `parent_id`, `type_id`) VALUES
(1, '女装 / 内衣', '', '', '', NULL, 1, '/public/upload/20200907/a9a2ebdfc574b7ba4f63111b725173a1.jpg', '', 1, 0, 2),
(2, '浪漫裙装', '', '', '', NULL, 0, NULL, '', 50, 1, 1),
(3, '美妆 / 护理', '', '', '', NULL, 1, '/public/upload/20200907/6720d04938b02fad0a8df2468a954b98.jpg', '', 3, 0, 0),
(4, '生活电器', '', '', '', NULL, 1, '/public/static/index/images/thumb.jpg', '', 4, 0, 0),
(5, '护肤品', '', '', '', NULL, 1, '/public/upload/20200907/3dfe3c4b167f688385657d72d1321bc5.jpg', '', 50, 3, 0),
(6, '家居服', '', '', '', NULL, 1, '/public/upload/20200907/bf429889460c7a17b6521c201ef06b18.jpg', '', 50, 1, 0),
(7, '彩妆', '', '', '', NULL, 1, '/public/upload/20200907/59feb5c76f25fc55545541c381f6fb7f.jpg', '', 50, 3, 0),
(8, '洗衣机', '', '', '', NULL, 1, NULL, '', 50, 4, NULL),
(9, '电冰箱', '', '', '', NULL, 1, NULL, '', 50, 4, NULL),
(10, '家居 / 建材', '', '', '', NULL, 1, NULL, '', 50, 0, NULL),
(11, '母婴 / 玩具', '', '', '', NULL, 1, NULL, '', 50, 0, NULL),
(12, '图书 / 音像', '', '', '', NULL, 1, NULL, '', 30, 0, NULL),
(13, '零食 / 茶酒', '', '', '', NULL, 1, '/public/static/index/images/thumb.jpg', '', 5, 0, 0),
(14, '手机 / 数码', '', '', '', NULL, 1, '/public/upload/20200907/5feb28d16702eaf24bf85ebccc954d2d.jpg', '', 2, 0, 0),
(15, '腕表 / 首饰', '', '', '', NULL, 1, '/public/static/index/images/thumb.jpg', '', 6, 0, 0),
(16, '小米', '', '', '', NULL, 1, NULL, '', 50, 18, 1),
(17, '荣耀', '', '', '', NULL, 1, NULL, '', 50, 18, NULL),
(18, '热门手机', '', '', '', NULL, 1, NULL, '', 50, 14, 1),
(19, '电脑整机', '', '', '', NULL, 1, NULL, '', 50, 14, NULL),
(20, '笔记本', '', '', '', NULL, 1, NULL, '', 50, 19, NULL),
(21, '平板电脑', '', '', '', NULL, 1, NULL, '', 50, 19, NULL),
(22, '台式机', '', '', '', NULL, 1, NULL, '', 50, 19, NULL),
(23, '一体机', '', '', '', NULL, 1, NULL, '', 50, 19, NULL),
(24, '游戏本', '', '', '', NULL, 1, NULL, '', 50, 19, NULL),
(25, 'iPad', '', '', '', NULL, 1, NULL, '', 50, 19, NULL),
(26, 'Iphone', '', '', '', NULL, 1, NULL, '', 50, 18, NULL),
(27, '魅族', '', '', '', NULL, 1, NULL, '', 50, 18, NULL),
(28, '华为', '', '', '', NULL, 1, NULL, '', 50, 18, NULL),
(29, 'OPPO', '', '', '', NULL, 1, NULL, '', 50, 18, NULL),
(30, '智能数码', '', '', '', NULL, 1, NULL, '', 50, 14, NULL),
(31, '智能设备', '', '', '', NULL, 1, NULL, '', 50, 30, NULL),
(32, '智能手表', '', '', '', NULL, 1, NULL, '', 50, 30, NULL),
(33, '智能手环', '', '', '', NULL, 1, NULL, '', 50, 30, NULL),
(34, 'VR眼镜', '', '', '', NULL, 1, NULL, '', 50, 30, NULL),
(35, '智能摄像', '', '', '', NULL, 1, NULL, '', 50, 30, NULL),
(36, '智能健康', '', '', '', NULL, 1, NULL, '', 50, 30, NULL),
(37, '智能机器人', '', '', '', NULL, 1, NULL, '', 50, 30, NULL),
(38, '硬件存储', '', '', '', NULL, 1, NULL, '', 50, 14, NULL),
(39, '显示器', '', '', '', NULL, 1, NULL, '', 50, 38, NULL),
(40, '机械键盘', '', '', '', NULL, 1, NULL, '', 50, 38, NULL),
(41, '固态硬盘', '', '', '', NULL, 1, NULL, '', 50, 38, NULL),
(42, 'CPU', '', '', '', NULL, 1, NULL, '', 50, 38, NULL),
(43, '显卡', '', '', '', NULL, 1, NULL, '', 50, 38, NULL),
(44, '主板', '', '', '', NULL, 1, NULL, '', 50, 38, NULL),
(45, '高速U盘', '', '', '', NULL, 1, NULL, '', 50, 38, NULL),
(46, '路由器', '', '', '', NULL, 1, NULL, '', 50, 38, NULL),
(47, '摄影摄像', '', '', '', NULL, 1, NULL, '', 50, 14, NULL),
(48, '相机', '', '', '', NULL, 1, NULL, '', 50, 47, NULL),
(49, '单反', '', '', '', NULL, 1, NULL, '', 50, 47, NULL),
(50, '单电微单', '', '', '', NULL, 1, NULL, '', 50, 47, NULL),
(51, '摄像机', '', '', '', NULL, 1, NULL, '', 50, 47, NULL),
(52, '自拍神器', '', '', '', NULL, 1, NULL, '', 50, 47, NULL),
(53, '拍立得', '', '', '', NULL, 1, NULL, '', 50, 47, NULL),
(54, '镜头', '', '', '', NULL, 1, NULL, '', 50, 47, NULL),
(55, '自拍杆', '', '', '', NULL, 1, NULL, '', 50, 47, NULL),
(56, '影音娱乐', '', '', '', NULL, 1, NULL, '', 50, 14, NULL),
(57, '耳机', '', '', '', NULL, 1, NULL, '', 50, 56, NULL),
(58, '天猫魔盒', '', '', '', NULL, 1, NULL, '', 50, 56, NULL),
(59, '数码影音', '', '', '', NULL, 1, NULL, '', 50, 56, NULL),
(60, '家庭影院', '', '', '', NULL, 1, NULL, '', 50, 56, NULL),
(61, '蓝牙耳机', '', '', '', NULL, 1, NULL, '', 50, 56, NULL),
(62, '网络播放器', '', '', '', NULL, 1, NULL, '', 50, 56, NULL),
(63, '精选上装', '', '', '', NULL, 1, '/public/upload/20200907/12e2badae06d0be8dd1f6186c4142ea6.jpg', '', 50, 1, 2),
(64, '女士下装', '', '', '', NULL, 1, '/public/upload/20200907/bc7a1f58d4d737ffc2842f9483f2d7b0.jpg', '', 50, 1, 0),
(65, '特色女装', '', '', '', NULL, 1, '/public/upload/20200907/5b999d2f7a73e7616fe6f86e8b9eab4b.jpg', '', 50, 1, 0),
(66, '文胸塑身', '', '', '', NULL, 1, '/public/upload/20200907/049adc94a689a1ec4bccba90c43e719a.jpg', '', 50, 1, 0),
(67, '毛呢外套', '', '', '', NULL, 1, NULL, '', 50, 63, NULL),
(68, '羽绒服', '', '', '', NULL, 1, NULL, '', 50, 63, NULL),
(69, '棉服', '', '', '', NULL, 1, NULL, '', 50, 63, NULL),
(70, '丝绒卫衣', '', '', '', NULL, 1, NULL, '', 50, 63, NULL),
(71, '毛针织衫', '', '', '', NULL, 1, NULL, '', 50, 63, NULL),
(72, '皮毛一体', '', '', '', NULL, 1, NULL, '', 50, 63, NULL),
(73, '皮草', '', '', '', NULL, 1, NULL, '', 50, 63, NULL),
(74, '毛衣', '', '', '', NULL, 1, '/public/static/index/images/thumb.jpg', '', 50, 63, 2),
(75, '衬衫', '', '', '', NULL, 1, NULL, '', 50, 63, NULL),
(76, '卫衣', '', '', '', NULL, 1, '/public/static/index/images/thumb.jpg', '', 50, 63, 2),
(77, '针织衫', '', '', '', NULL, 1, NULL, '', 50, 63, NULL),
(78, 'T恤', '', '', '', NULL, 1, NULL, '', 50, 63, NULL),
(79, '短外套', '', '', '', NULL, 1, NULL, '', 50, 63, NULL),
(80, '小西装', '', '', '', NULL, 1, NULL, '', 50, 63, NULL),
(81, '风衣', '', '', '', NULL, 1, NULL, '', 50, 63, NULL),
(82, '连衣裙', '', '', '', NULL, 1, NULL, '', 50, 2, NULL),
(83, '半身裙', '', '', '', NULL, 1, NULL, '', 50, 2, NULL),
(84, 'A字裙', '', '', '', NULL, 1, NULL, '', 50, 2, NULL),
(85, '荷叶边裙', '', '', '', NULL, 1, NULL, '', 50, 2, NULL),
(86, '大摆裙', '', '', '', NULL, 1, NULL, '', 50, 2, NULL),
(87, '包臀裙', '', '', '', NULL, 1, NULL, '', 50, 2, NULL),
(88, '百褶裙', '', '', '', NULL, 1, NULL, '', 50, 2, NULL),
(89, '长袖', '', '', '', NULL, 1, NULL, '', 50, 2, NULL),
(90, '连衣裙', '', '', '', NULL, 1, NULL, '', 50, 2, NULL),
(91, '棉麻连衣裙', '', '', '', NULL, 1, NULL, '', 50, 2, NULL),
(92, '牛仔裙', '', '', '', NULL, 1, NULL, '', 50, 2, NULL),
(93, '蕾丝连衣裙', '', '', '', NULL, 1, NULL, '', 50, 2, NULL),
(94, '真丝连衣裙', '', '', '', NULL, 1, NULL, '', 50, 2, NULL),
(95, '印花连衣裙', '', '', '', NULL, 1, NULL, '', 50, 2, NULL),
(96, '春夏家居服', '', '', '', NULL, 1, NULL, '', 50, 6, NULL),
(97, '纯棉家居服', '', '', '', NULL, 1, NULL, '', 50, 6, NULL),
(98, '莫代尔家居服', '', '', '', NULL, 1, NULL, '', 50, 6, NULL),
(99, '真丝家居服', '', '', '', NULL, 1, NULL, '', 50, 6, NULL),
(100, '春夏睡裙', '', '', '', NULL, 1, NULL, '', 50, 6, NULL),
(101, '男士家居服', '', '', '', NULL, 1, NULL, '', 50, 6, NULL),
(102, '情侣家居服', '', '', '', NULL, 1, NULL, '', 50, 6, NULL),
(103, '性感睡裙', '', '', '', NULL, 1, NULL, '', 50, 6, NULL),
(104, '休闲裤', '', '', '', NULL, 1, NULL, '', 50, 64, NULL),
(105, '阔腿裤', '', '', '', NULL, 1, NULL, '', 50, 64, NULL),
(106, '牛仔裤', '', '', '', NULL, 1, NULL, '', 50, 64, NULL),
(107, '打底裤', '', '', '', NULL, 1, NULL, '', 50, 64, NULL),
(108, '开叉运动裤', '', '', '', NULL, 1, NULL, '', 50, 64, NULL),
(109, '哈伦裤', '', '', '', NULL, 1, NULL, '', 50, 64, NULL),
(110, '背带裤', '', '', '', NULL, 1, NULL, '', 50, 64, NULL),
(111, '小脚裤', '', '', '', NULL, 1, NULL, '', 50, 64, NULL),
(112, '西装裤', '', '', '', NULL, 1, NULL, '', 50, 64, NULL),
(113, '短裤', '', '', '', NULL, 1, NULL, '', 50, 64, NULL),
(114, '时尚套装', '', '', '', NULL, 1, NULL, '', 50, 65, NULL),
(115, '休闲套装', '', '', '', NULL, 1, NULL, '', 50, 65, NULL),
(116, '日系女装', '', '', '', NULL, 1, NULL, '', 50, 65, NULL),
(117, '精选妈妈装', '', '', '', NULL, 1, NULL, '', 50, 65, NULL),
(118, '大码女装', '', '', '', NULL, 1, NULL, '', 50, 65, NULL),
(119, '职业套装', '', '', '', NULL, 1, NULL, '', 50, 65, NULL),
(120, '优雅旗袍', '', '', '', NULL, 1, NULL, '', 50, 65, NULL),
(121, '精致礼服', '', '', '', NULL, 1, NULL, '', 50, 65, NULL),
(122, '婚纱', '', '', '', NULL, 1, NULL, '', 50, 65, NULL),
(123, '唐装', '', '', '', NULL, 1, NULL, '', 50, 65, NULL),
(124, '小码女装', '', '', '', NULL, 1, NULL, '', 50, 65, NULL),
(125, '光面文胸', '', '', '', NULL, 1, NULL, '', 50, 66, NULL),
(126, '运动文胸', '', '', '', NULL, 1, NULL, '', 50, 66, NULL),
(127, '美背文胸', '', '', '', NULL, 1, NULL, '', 50, 66, NULL),
(128, '聚拢文胸', '', '', '', NULL, 1, NULL, '', 50, 66, NULL),
(129, '大杯文胸', '', '', '', NULL, 1, NULL, '', 50, 66, NULL),
(130, '轻薄塑身', '', '', '', NULL, 1, NULL, '', 50, 66, NULL),
(131, '家居服', '', '', '', '', 1, '/public/upload/20200907/6502a54382a96c2f370933c280a8456f.jpg', '', 50, 1, 1),
(132, '精选上衣', '', '', '', '', 1, '/public/upload/20200907/be5921de5ee20e4932305a8dabd8d7d8.jpg', '', 50, 1, 1),
(133, '家居服A', '', '', '', NULL, 1, '/public/static/index/images/thumb.jpg', '', 50, 6, 2),
(134, '家居服B', '', '', '', NULL, 1, '/public/static/index/images/thumb.jpg', '', 50, 6, 2),
(138, '进口零食', '', '', '', NULL, 1, '/public/static/index/images/thumb.jpg', '', 50, 13, 1),
(139, '休闲零食', '', '', '', NULL, 1, '/public/static/index/images/thumb.jpg', '', 50, 13, 1),
(140, '酒类', '', '', '', NULL, 1, '/public/static/index/images/thumb.jpg', '', 50, 13, 1),
(141, '茶叶1', '', '', '', NULL, 1, '/public/static/index/images/thumb.jpg', '', 50, 13, 1),
(142, '茶叶2', '', '', '', NULL, 1, '/public/static/index/images/thumb.jpg', '', 50, 13, 1),
(143, '', '', '', '', NULL, 1, '/public/static/index/images/thumb.jpg', '', 50, 13, 1),
(144, '大牌乐器', '', '', '', NULL, 1, '/public/static/index/images/thumb.jpg', '', 50, 12, 1),
(145, '儿童读书', '', '', '', NULL, 1, '/public/static/index/images/thumb.jpg', '', 50, 12, 1),
(146, '儿童读物1', '', '', '', NULL, 1, '/public/static/index/images/thumb.jpg', '', 50, 12, 1),
(148, '儿童读书2', '', '', '', NULL, 1, '/public/static/index/images/thumb.jpg', '', 50, 12, 1),
(149, '儿童读书3', '', '', '', NULL, 1, '/public/static/index/images/thumb.jpg', '', 50, 12, 1),
(150, '玩具', '', '', '', NULL, 1, '/public/static/index/images/thumb.jpg', '', 50, 11, 1),
(151, '童装', '', '', '', NULL, 1, '/public/static/index/images/thumb.jpg', '', 50, 11, 1),
(155, '电视机', '', '', '', NULL, 1, '/public/static/index/images/thumb.jpg', '', 50, 4, 3),
(153, '婴儿服', '', '', '', NULL, 1, '/public/static/index/images/thumb.jpg', '', 50, 11, 1),
(154, '奶粉', '', '', '', NULL, 1, '/public/static/index/images/thumb.jpg', '', 50, 11, 1),
(156, '液晶电视机', '', '', '', NULL, 1, '/public/static/index/images/thumb.jpg', '', 50, 155, 3),
(157, '灯饰照明', '', '', '', NULL, 1, '/public/static/index/images/thumb.jpg', '', 50, 4, 0),
(158, '灯泡', '', '', '', NULL, 1, '/public/static/index/images/thumb.jpg', '', 50, 157, 0),
(159, '办公文教', '', '', '', NULL, 1, '/public/static/index/images/thumb.jpg', '', 50, 14, 0),
(160, '婴儿玩具', '', '', '', NULL, 1, '/public/static/index/images/thumb.jpg', '', 50, 150, 0);
--
-- 转储表的索引
--
--
-- 表的索引 `qing_category`
--
ALTER TABLE `qing_category`
ADD PRIMARY KEY (`id`);
--
-- 在导出的表使用AUTO_INCREMENT
--
--
-- 使用表AUTO_INCREMENT `qing_category`
--
ALTER TABLE `qing_category`
MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '栏目id', AUTO_INCREMENT=161;
COMMIT;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
代码
use app\common\model\Category as CategoryModel;//引用category这张表并取名为CategoryModel
$category_model=new CategoryModel();
$indexCate=$category_model->getNavCateData();
halt($indexCate);
public function getNavCateData(){
$data=array();
$allcate=$this->order('listorder asc')->field('id,parent_id,cate_name')->select()->toArray();
foreach ($allcate as $k => $v) {
if($v['parent_id']==0){
foreach ($allcate as $k1 => $v1) {
if($v1['parent_id']==$v['id']){
foreach ($allcate as $k2 => $v2) {
if($v2['parent_id']==$v1['id']){
$v1['children'][]=$v2;
}
}
$v['children'][]=$v1;
}
}
$data[]=$v;
}
}
return $data;
}
2.效果图
总结
谢谢大家的观看,谢谢!