10-6写不出来,可以指点下吗/
来源:10-6 商品列表页API开发
慕的地8283736
2020-04-10
已经按照老师的最优解来写了,也解决category表中path的问题:
http://coding.imooc.com/learn/questiondetail/172103.html
controller\category
public function search()
{
//获取数据id
$id = input('param.cid',0,'intval');
if (!$id) //如果为0,返回首页
{
return Show::success();
}
$result = (new CategoryBusiness())->searchList($id);
return Show::success($result);
}
business\category:
public function searchList($id)
{
$field = "path";
$res = $this->model->pathNumber($id,$field);
if (!$res)
{
return [];
}
$arr = explode(",",$res['path']);
try {
$result1 = $this->model->searchList($arr);
} catch (\Exception $e)
{
Log::error("获取数据searchList失败{$e->getMessage()}");
return [];
}
foreach ($result1 as $v)
{
$items[$v['id']] = $v;
}
//dump($items);exit;
$result['name'] = $items[$id]['name'];
if (count($arr) == 2)
{
$result['focus_ids'] = [$arr[1]] ;
foreach ($items as $v)
{
unset($v['pid']);
$array['list'][] = $v;
}
} elseif (count($arr) == 3)
{
$result['focus_ids'] = [$arr[1],$arr[2]];
foreach ($items as $v)
{
//unset($v['pid']);
// $array['list'][] = $v['pid'] == $arr[0] ? : ;
}
}
//dump();exit;
$result['list'] = $array['list'];
return $result;
//dump($result);exit;
}
mysql\business
public function searchList($arr)
{
$num = count($arr);
if ($num == 2)
{
$where = [
'pid' => $arr[0],
'status' => config("status.mysql.table_normal"),
];
} elseif($num == 3)
{
$where[] = ['pid','in',[$arr[0],$arr[1]]];
$where[] = ['status','=',config('status.mysql.table_normal')];
}
$field = 'id,name,pid';
$order = [
'id' => 'desc',
'listorder' => 'desc',
];
$res = $this->where($where)
->field($field)
->order($order)
->select();
//echo $this->getLastSql();exit;
return $res = $res->toArray();
}
效果·
差强人意,搞的有点乱了,能看一下正确的代码是怎么样的吗?
写回答
1回答
-
慕的地8283736
提问者
2020-04-11
写出来来谢谢,就不增加问题
012020-04-12
相似问题