国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

TP框架實現獲取無限極分類的樹

2019-11-11 04:42:59
字體:
來源:轉載
供稿:網友
看了很多別人的實現方法,都沒看明白,所以自己寫一個。
數據庫結構如下:
model里面這樣子
<?php/** * Created by PhpStorm. * User: 王柏建 * Date: 2016/10/09 * Time: 11:32 */namespace Api/Model;use Think/Model;class TypeModel extends Model{    //通過admin_id獲取分類    public static function getTypeByAdminId($admin_id){        return M('type')->where(array("admin_id"=>$admin_id))->select();    }    //通過條件獲取一個分類    public static function getTypeByWhere($where){        return M('type')->where($where)->find();    }        //通過條件獲取部分分類    public static function getAllTypeByWhere($where){        return M('type')->where($where)->field("clothes_type_id,clothes_type_father_id,clothes_type_name,clothes_type_icon_url")->select();    }}
最后這里是controller里面的寫法。
<?phpnamespace Api/Controller;use Api/Model/TypeModel;use Think/Controller;class TreeController extends Controller {    //記錄層    public $div = [];    //記錄樹    public $tree = [];    //返回樹    public function returnTree(){        $tree = [];        //獲取目前已經有的結構        $this->getChild(0);  //從第一級開始        //dump($this->tree);   //這里是輸出數組        $this->AjaxReturn($this->tree,"json");   //輸出json    }    //獲取某個分類的子分類以及下面的服飾    public function getChild($clothes_type_father_id){        $types = TypeModel::getAllTypeByWhere(array("clothes_type_father_id"=>$clothes_type_father_id));        if(!$types){            array_pop($this->div);            return;        }        if($clothes_type_father_id == 0){            $this->div[] = 0;        }        else{            $this->div[] = TypeModel::getTypeByWhere(array('clothes_type_id'=>$clothes_type_father_id))['clothes_type_id'];        }        //將獲取到的東西放到$this->tree里面        $this->putData($this->tree,$this->div,$types);        //循環這一級里面的類,再獲取其子類和下面的服飾        foreach($types as $t){            $this->getChild($t['clothes_type_id']);        }    }    //插入數據到特定下標    public function putData(&$array,$down,$data){        $j = [];        $k = 0;        foreach($down as $d){            if($j == null){                $j[$k] = &$array[$d];            }            else{                $j[$k] = &$j[$k++][$d]['son'];            }        }        $count = count($j)-1;        foreach($data as $d){            $j[$count][$d['clothes_type_id']] = $d;        }        unset($j);        unset($k);    }}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 莱芜市| 遂宁市| 宣威市| 苏尼特右旗| 金阳县| 莱州市| 兴安县| 阜南县| 英超| 吉林市| 汨罗市| 瑞安市| 茶陵县| 万源市| 蓬溪县| 双辽市| 和静县| 雷山县| 南平市| 英超| 原阳县| 遵义县| 藁城市| 秭归县| 台州市| 上饶县| 洱源县| 浮山县| 驻马店市| 托里县| 澳门| 平塘县| 依安县| 四川省| 澄江县| 望都县| 潍坊市| 靖安县| 合川市| 甘肃省| 石家庄市|