1. 程式人生 > 其它 >記一次 PHP 省市縣三級聯動 資料庫取值

記一次 PHP 省市縣三級聯動 資料庫取值

   /**
     * Notes:省市縣三級聯動
     * Created by depressiom
     * Date: 2022年4月14日
     */
    public function getCityData(){
        //獲取一級
        $cityList = M('area')->where(['type'=>0])->select();  // 根據type 取一級
        $data = array();
        if(count($cityList)>0){
            foreach ($cityList as $v) {
                $data[$v['id']] = array(
                    "id" => $v['id'],
                    "value" => $v['area_code'],
                    "label" => $v['name'],
                    "children" => array()
                );
                //獲取二級
                $cityTow = M('area')->where(['type' => 1,'parent_code'=>$v['id']])->select();  // 資料庫通過關聯的 一級id為 二級的父code  type為1 表示市區
                if(count($cityTow)>0) {
                    foreach ($cityTow as $l) {
                        $data[$v['id']]['children'][$l['id']] = array(
                            "id" => $l['id'],
                            "value" => $l['area_code'],  // 這裡因為第一次取得是$v的值 所以出現二級與一級name一樣
                            "label" => $l['name'],
                            "children" => array()
                        );
                        //獲取三級
                        $cityThree = M('area')->where(['type' => 2, 'parent_code' => $l['id']])->select();  // 三級同理
                        if(count($cityTow)>0) {
                            foreach ($cityThree as $t) {
                                $data[$v['id']]['children'][$l['id']]['children'][] = array(
                                    "id" => $t['id'],
                                    "value" => $t['area_code'],
                                    "label" => $t['name'],
                                );
                            }
                        }else{
                            $msg = array(
                                "status" => -1,
                                "msg" => "獲取縣資料失敗!",
                                "result" => null
                            );
                            $this->ajaxReturn($msg);
                            exit ();
                        }
                    }
                }else{
                    $msg = array(
                        "status" => -1,
                        "msg" => "獲取市資料失敗!", //.json_encode($cityTow,true) 資料不正常是前端展示檢視結果
                        "result" => null
                    );
                    $this->ajaxReturn($msg);
                    exit ();
                }
            }
        }else{
            $msg = array(
                "status" => -1,
                "msg" => "獲取省資料失敗!",
                "result" => null
            );
            $this->ajaxReturn($msg);
            exit ();
        }

        if (count($data)<=0) {
            $msg = array(
                "status" => -1,
                "msg" => "資料出錯",
                "result" => null
            );
            $this->ajaxReturn($msg);
            exit ();
        }

        //格式化資料
        $res = array();
        foreach ($data as $val) {
            $item = array();
            foreach ($val['children'] as $v) {
                $item[] = $v;
            }
            $res[] = array(
                'id' => $val['id'],
                "value" => $val['value'],  // 這裡是格式化資料,因為我第一層格式已經將$data裡面的欄位名字改變  所以用已改變的值,就是因為這裡導致找了半小時
                "label" => $val['label'],
                'children' => $item
            );
        }

        $msg = array(
            "status" => 1,
            "msg" => "資料獲取成功",
            "result" => $res,
            "data"=>$data,
        );
        $this->ajaxReturn($msg);
    }

感謝大佬的分享 原作者地址