问题:为什么会输出手机下的子类呢?
因为postman截图哪个我设置的id=1。看看sql吧
public function getSubcats($id){//看$id等于pid字段看看是否有值
if($id=='null'){//如果有值代表他不是顶级分类,为什么呢?因为因为顶级分类的pid固定是0嘛对吧.
$pid = Db::name('cat')->where('is_show',1)->where('pid',0)->value('id');//找到某个顶级分类id的值
$cats = Db::name('cat')->where('is_show',1)->where('pid',$pid)->select()->toArray();//找到某个顶级分类其下的所有子类输出
}else{//如果pid有值,代表他不是顶级分类。为什么呢?因为顶级分类的pid固定是0嘛对吧.
$cats = Db::name('cat')->where('is_show',1)->where('pid',$id)->select()->toArray();//找到($id==pid字段)==所对应的id(子类)了呀.
}
if($cats){
$data = ['code'=>200,'msg'=>'success','data'=>$cats];
}else{
$data = ['code'=>440,'msg'=>'no cats'];
}
return json($data);
}
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
use think\facade\Route;
Route::get('subcats/:id', 'Category/getSubcats');
//
-- phpMyAdmin SQL Dump
-- version 4.8.5
-- https://www.phpmyadmin.net/
--
-- 主机: localhost
-- 生成日期: 2022-05-23 18:02:43
-- 服务器版本: 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`
--
-- --------------------------------------------------------
--
-- 表的结构 `cat`
--
CREATE TABLE `cat` (
`id` int(10) UNSIGNED NOT NULL,
`pid` int(10) UNSIGNED NOT NULL,
`cat_name` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`cat_img` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`is_show` tinyint(4) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- 转存表中的数据 `cat`
--
INSERT INTO `cat` (`id`, `pid`, `cat_name`, `cat_img`, `is_show`) VALUES
(1, 0, '手机', '/storage/category/bc\\91231321a92a7a6a6db99fa7db8f37.png', 1),
(7, 0, '厨具', '/storage/category/01\\c7232eb931fa3dd40f8b4946bd27fa.jpg', 1),
(8, 7, '电饭锅', '/storage/category/2b\\edd18255c4efcfc4a9982ce33671be.jpg', 1),
(9, 0, '服装', '/storage/category/f6\\655e2bed6b7bea027fb69a3c11a27c.jpg', 1),
(10, 9, '女装', '/storage/category/84\\f9e13fa3a6093990bf3de4e2da74a0.jpg', 1),
(11, 1, '华为', '/storage/category/b4\\a403d9874ed426bd5210a2f42c33f0.jpg', 1);
--
-- 转储表的索引
--
--
-- 表的索引 `cat`
--
ALTER TABLE `cat`
ADD PRIMARY KEY (`id`);
--
-- 在导出的表使用AUTO_INCREMENT
--
--
-- 使用表AUTO_INCREMENT `cat`
--
ALTER TABLE `cat`
MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=12;
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 */;