头像

php 读取 mysql 组合输出json数据

资源/模板错误反馈 收集员 2015-12-15浏览(1670)   

php 读取 mysql  组合输出json数据
查看演示 下载资源: 232下载资源下载积分:0
public function list_index(){
        $list=M('goods_cats');
        $data=$list->where('parentId = 0 and isShow="1"')->order('catSort ASC')->select();
        //组合输出顶级分类
        //dump($data);
        $x='0';
        while($x<count($data)) {
            //echo "这个数字是:$x <br>";
            $responce->error_code = '0';
            $opt = '';
            $responce->data['categoryList'][] = array(
                    "id" => $data[$x]['catId'],
                    "name" => $data[$x]['catName'],
                    "level" => "1",
                    "pid" => "0",
                    "is_delete" => "0",
                    "is_standard" => "",
                    "sequence" => $x
            );
            $responce->error_msg="";
            $responce->redirect_url="http://www.baidu.com";
            $x++;
              
        }
        //dump($data);
        $this->ajaxReturn($responce);
        //echo '{"error_code":0,"data":{"categoryList":[{"id":"30","name":"\u852c\u83dc\u7c7b","level":"1","pid":"0","is_delete":"0","is_standard":"N","sequence":"7"},{"id":"30","name":"\u852c\u83dc\u7c7b","level":"1","pid":"0","is_delete":"0","is_standard":"N","sequence":"7"},{"id":"30","name":"\u852c\u83dc\u7c7b","level":"1","pid":"0","is_delete":"0","is_standard":"N","sequence":"7"},{"id":"98","name":"\u6c34\u679c\u7c7b","level":"1","pid":"0","is_delete":"0","is_standard":"N","sequence":"7"},{"id":"31","name":"\u8089\u79bd\u86cb\u7c7b","level":"1","pid":"0","is_delete":"0","is_standard":"N","sequence":"6"},{"id":"33","name":"\u6c34\u4ea7\u7c7b","level":"1","pid":"0","is_delete":"0","is_standard":"N","sequence":"5"},{"id":"92","name":"\u8c03\u5473\u54c1\u7c7b","level":"1","pid":"0","is_delete":"0","is_standard":"N","sequence":"4"},{"id":"68","name":"\u7cae\u6cb9\u7c7b","level":"1","pid":"0","is_delete":"0","is_standard":"N","sequence":"3"},{"id":"35","name":"\u9152\u6c34\u7c7b","level":"1","pid":"0","is_delete":"0","is_standard":"N","sequence":"2"},{"id":"36","name":"\u53a8\u623f\u7528\u54c1\u7c7b","level":"1","pid":"30","is_delete":"0","is_standard":"N","sequence":"1"}]},"error_msg":"","redirect_url":""}';
    }

这里用的tp框架做演示。


首先读取了数据库。

$list=M('goods_cats');
        $data=$list->where('parentId = 0 and isShow="1"')->order('catSort ASC')->select();
        //这里获取了需要的数据。

当我们获得了数据之后直接用

$this->ajaxReturn($responce);
//将获得的数据转化为json

一般来有这句就完成了,但是前端接受json不一定就是你数据默认组合出来的数据。

$x='0';
        while($x<count($data)) {
            //echo "这个数字是:$x <br>";
            $responce->error_code = '0';
            $opt = '';
            $responce->data['categoryList'][] = array(
                    "id" => $data[$x]['catId'],
                    "name" => $data[$x]['catName'],
                    "level" => "1",
                    "pid" => "0",
                    "is_delete" => "0",
                    "is_standard" => "",
                    "sequence" => $x
            );
            $responce->error_msg="";
            $responce->redirect_url="http://www.baidu.com";
            $x++;
              
        }

我们将获得的默认数据$data转化为我们需要的数据

{"error_code":0,"data":{"categoryList":[{"id":"30","name":"\u852c\u83dc\u7c7b","level":"1","pid":"0","is_delete":"0","is_standard":"N","sequence":"7"},{"id":"30","name":"\u852c\u83dc\u7c7b","level":"1","pid":"0","is_delete":"0","is_standard":"N","sequence":"7"},{"id":"30","name":"\u852c\u83dc\u7c7b","level":"1","pid":"0","is_delete":"0","is_standard":"N","sequence":"7"},{"id":"98","name":"\u6c34\u679c\u7c7b","level":"1","pid":"0","is_delete":"0","is_standard":"N","sequence":"7"},{"id":"31","name":"\u8089\u79bd\u86cb\u7c7b","level":"1","pid":"0","is_delete":"0","is_standard":"N","sequence":"6"},{"id":"33","name":"\u6c34\u4ea7\u7c7b","level":"1","pid":"0","is_delete":"0","is_standard":"N","sequence":"5"},{"id":"92","name":"\u8c03\u5473\u54c1\u7c7b","level":"1","pid":"0","is_delete":"0","is_standard":"N","sequence":"4"},{"id":"68","name":"\u7cae\u6cb9\u7c7b","level":"1","pid":"0","is_delete":"0","is_standard":"N","sequence":"3"},{"id":"35","name":"\u9152\u6c34\u7c7b","level":"1","pid":"0","is_delete":"0","is_standard":"N","sequence":"2"},{"id":"36","name":"\u53a8\u623f\u7528\u54c1\u7c7b","level":"1","pid":"30","is_delete":"0","is_standard":"N","sequence":"1"}]},"error_msg":"","redirect_url":""}

仔细看我们组合的数据 多级数组都可以完成组合,在前端已经写好的情况下,配和前端就可以这么做。

最后自然是将我们组合的数据 用php函数json_encode() 或用tp函数输出成json

积分:新注册可得1积分|评论每次可得1积分每天5次[充值1元=10积分]AD:Php/Tp5程序开发二次开发联系QQ839070295
评论1
头像
  • 头像1楼
    12-23 12:16
    bibi
    积分!

模板缺失不完整!联系我!