1.先要递归查询数据 数据根据实际情况完成查询
1 //查询分类 2 public static function getOneCate() 3 { 4 $cateData = self::select(); 5 return self::getTree($cateData,0); 6 } 7 //展示层级查询 8 public static function getTree($data,$pid=0) 9 { 10 $result = []; 11 if (empty($data)){ 12 return $result; 13 } 14 foreach ($data as $key=>$item) { 15 if ($item[‘pid‘] == $pid){ 16 $item[‘child‘] = self::getTree($data,$item[‘id‘]); 17 $result[] = $item; 18 } 19 } 20 21 return $result; 22 }
2.将数据分层的展示到页面上
<select name="pid"> <option value="0">作为*分类</option> {foreach $data as $key=>$cate} <option value="{$cate.id}">{$cate.cate_name}</option> {foreach $cate.child as $k=>$val} <option value="{$val.id}">{:str_repeat(" ",$val.sort)}{$val.cate_name}</option> {foreach $val.child as $ke=>$vo} <option value="{$vo.id}">{:str_repeat(" ",$val.sort*2)}{$vo.cate_name}</option> {/foreach} {/foreach} {/foreach} </select>
效果图如下