如何在mysql响应中使用活动记录在php中编写嵌套的JSON

时间:2017-10-18 18:01:39

标签: php mysql json codeigniter

我有一个内置在活动记录中的PHP查询,并希望获得嵌套的JSON响应。

我使用Codeigniter MVC作为我的PHP框架。

在我的控制器

$.ajax({
  dataType: "json",
  url: url,
  data: data,
  success: function(data) {
    console.log('pot', data.pot);
  }
});

我的模型是此查询:

$data['all_json'] = $this->json_model->get_my_json($var1);

$result_array = array();

foreach ($data as $row) {
    $result_array[] = array();
}

exit;

我想构建一个JSON文档,如下所示:

以下是常规回报:

public function get_my_json($var1) 
{
    $this->db->select('column1,column2,column3');
    $this->db->from('table');
    $this->db->where('column1',$var1);
    $query = $this->db->get();
    $result = $query->result();

    print json_encode($result,JSON_PRETTY_PRINT);
}

相应的架构doc:

[
    {
        "id": 2,
        "name": "column1",
        "price": column2,
        "tags": ["cold", "ice"],
        "warehouseLocation": {
            "latitude": column3,
            "longitude": column4
        }
    },
    {
        "id": 3,
        "name": "column1",
        "price": column2,
        "warehouseLocation": {
            "latitude": column3,
            "longitude": column4
        }
    }
]

0 个答案:

没有答案