【问题标题】:Codeigniter, How to display select query result in json format?Codeigniter,如何以 json 格式显示选择查询结果?
【发布时间】:2017-01-07 12:00:43
【问题描述】:

我有两张桌子 表 1:dr_country

table2:dr_city

我必须从两个表中获取数据。我所需的输出格式应如下所示:

[{"id":"1","country_name":"Australia","country_code":"61","iso_code":"AUS","city":
[{"id":"1","city_name":"sydney"},{"id":"2","city_name":"melbourne"},{"id":"3","city_name":"perth"},{"id":"4","city_name":"brisbane"}]},
{"id":"2","country_name":"Bangladesh","country_code":"880","iso_code":"BGD","city":
[{"id":"5","city_name":"dhaka"},{"id":"6","city_name":"chittagong"}]}]

我的模型方法如下:

public function countryAction()
{
    $this->db->select("*");
    $this->db->from('dr_country');
    $this->db->join('dr_city', 'dr_country.id = dr_city.country_id','left');

    $result = $this->db->get()->result_array();
   if($result)
   {
        print_r(json_encode($result));

    }
    else
    {
         $detail = array(
            'status'=>'unsucess',   
           );

          echo  json_encode($detail); 
    }
}

它产生如下输出:

[{"id":"1","country_name":"Australia","country_code":"61","iso_code":"AUS","city_name":"sydney","country_id":"1"},{"id":"2","country_name":"Australia","country_code":"61","iso_code":"AUS","city_name":"melbourne","country_id":"1"},{"id":"3","country_name":"Australia","country_code":"61","iso_code":"AUS","city_name":"perth","country_id":"1"},{"id":"4","country_name":"Australia","country_code":"61","iso_code":"AUS","city_name":"brisbane","country_id":"1"},{"id":"5","country_name":"Bangladesh","country_code":"880","iso_code":"BGD","city_name":"dhaka","country_id":"2"},{"id":"6","country_name":"Bangladesh","country_code":"880","iso_code":"BGD","city_name":"chittagong","country_id":"2"}]

因此,为了获得所需的 json 格式,我应该在模型函数中进行哪些更改,我是新手,提前致谢。

【问题讨论】:

  • dr_country.country_name,dr_city.city_name 分组

标签: php mysql json codeigniter


【解决方案1】:

在你的模型中试试这个

$countries = $this->db->get('dr_country')->result();
foreach ($countries as $key => $country) {
    $country->city = $this->db->get_where('dr_city', array('country_id' => $country->id))->result();
}
print_r(json_encode($countries));

【讨论】:

  • print_r(json_encode($country));我只为所有国家/地区获得一个城市(相同的悉尼)
  • [{"id":"1","country_name":"Australia","country_code":"61","iso_code":"AUS","city":[{"city_id ":"1","city_name":"sydney","country_id":"1"}]},{"id":"2","country_name":"Bangladesh","country_code":"880", "iso_code":"BGD","city":[{"city_id":"1","city_name":"sydney","country_id":"1"}]}] 这就是我得到的 city(sydney ) 对所有重复
【解决方案2】:

请尝试以下步骤:

  1. 获取所有国家 - 从 dr_country 中选择 *
  2. 遍历国家并获取每个国家/地区的城市

    foreach($countries as $c) { $countries[$city_id] = select * from dr_city where country_id = $c; }

    1. json 编码

【讨论】:

    【解决方案3】:

    您的预期输出永远不会与直接数据库一起出现。 因为数据库以 TABLE 格式提供输出。

    如果您想要您的预期输出,那么您应该执行 2 个 SQL 查询。 i) 获取国家 ii) 获取城市 并制作一个国家数组的foreach循环并查找与国家相关的所有城市 并制作输出数组。

    【讨论】:

      【解决方案4】:

      $countries = $this->db->get('dr_country')->result();
      foreach ($countries as $key => $country) {
          $country->city = $this->db->get_where('dr_city', array('country_id' => 
      $country->id))->result();
          $countryfinal[]=$country;
      }
      print_r(json_encode($countryfinal));

      【讨论】:

        【解决方案5】:
            $countries = $this->db->get('dr_country')->result();
        foreach ($countries as $key => $country) {
            $country->city = $this->db->get_where('dr_city', array('country_id' => 
        $country->id))->result();
            $countryfinal[]=$country;
        }
        print_r(json_encode($countryfinal));
        

        【讨论】:

          猜你喜欢
          • 2017-01-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-12-16
          相关资源
          最近更新 更多