其他分享
首页 > 其他分享> > tp5.0 实现数据在下拉框中层级展示

tp5.0 实现数据在下拉框中层级展示

作者:互联网

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("&nbsp;",$val.sort)}{$val.cate_name}</option>
                    {foreach $val.child as $ke=>$vo}
                        <option value="{$vo.id}">{:str_repeat("&nbsp;",$val.sort*2)}{$vo.cate_name}</option>
                    {/foreach}
            {/foreach}
        {/foreach}
    </select>

效果图如下

 

标签:tp5.0,层级,cate,val,item,foreach,data,下拉框,result
来源: https://www.cnblogs.com/gj210623/p/15260851.html