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

写出来来谢谢,就不增加问题

0
1
singwa
您好。我这两天写下 直接入git .
2020-04-12
共1条回复

全流程开发 TP6.0实战高并发电商服务系统

一课就能掌握TP6.0基础及运用,打造完整高并发的电商后端项目

1461 学习 · 1310 问题

查看课程