简体   繁体   English

在Codeigniter中生成JSON查询结果

[英]Generating JSON Query Result in Codeigniter

So i have database table named usermeta and have table structure like this : 所以我有一个名为usermeta的数据库表,并且具有这样的表结构:

-----------------------------------------------------------
| ummeta_id | user_id | meta_key    | meta_value          |
-----------------------------------------------------------
| 1         | 1       | fullname    | John Doe            |
| 2         | 1       | birthplace  | New York            |
| 3         | 1       | birthdate   | 1990/01/01          |
| 4         | 1       | mobile      | 0812-3456-7890      |
| 5         | 1       | email       | john.doe@mail.com   |
| 6         | 2       | fullname    | Jon Wick            |
| 7         | 2       | birthplace  | Washington DC       |
| 8         | 2       | birthdate   | 1985/10/21          |
| 9         | 2       | mobile      | 0890-1234-5678      |
| 10        | 2       | email       | wickjohn@mail.com   |

And i try to generate json data for all data from this database using Codeigniter (v 3.1.9) using Controller and Model. 我尝试使用Controller和Model使用Codeigniter(v 3.1.9)从该数据库生成所有数据的json数据。

This is my Model (model name: db_usermeta) 这是我的模型 (模型名称:db_usermeta)

function userslist()
{
   $query = $this->db->select('*')
                     ->from('usermeta')
                     ->get();
   return $query->result();
}

This is my Controller 这是我的控制器

public function userlist()
{
   header('Content-Type: application/json; charset=utf-8');
   $query = $this->db_usermeta->userslist();
   $json_data = array();
   foreach ($query as $key)
   {
      $json_data[$key->meta_key] = $key->meta_value;
   }
   echo json_encode($json_data);
}

The result when i open using my browser to check the json data using web developer tool is only show last record, in this case only show data from user_id 2, like this: 当我使用浏览器打开浏览器以使用Web Developer工具检查json数据时,结果仅显示最后一条记录,在这种情况下,仅显示user_id 2中的数据,如下所示:

{
  "fullname":"John Wick",
  "birthplace":"Washinton DC",
  "birthdate":"1985/10/21",
  "mobile":"0890-1234-5678",
  "email":"wickjohn@mail.com"
}

What I want to achieve is to show all json data nested like this: 我想要实现的是显示嵌套的所有json数据,如下所示:

"data": [
  {
    "fullname":"John Doe",
    "birthplace":"New York",
    "birthdate":"1990/01/01",
    "mobile":"0812-3456-7890",
    "email":"john.doe@mail.com"
  },
  {
    "fullname":"John Wick",
    "birthplace":"Washinton DC",
    "birthdate":"1985/10/21",
    "mobile":"0890-1234-5678",
    "email":"wickjohn@mail.com"
  }
]

How can i achieve this? 我怎样才能做到这一点? Did I make a mistake on my controller and model. 我在控制器和模型上犯了错误吗? I really appreciate your help. 非常感谢您的帮助。

Since the meta key fullname is same for both records, you need to change the key name to something unique 由于两个记录的元密钥fullname都相同,因此您需要将密钥名更改为唯一的名称

foreach ($query as $key)
   {
      $json_data[$key->meta_key] = $key->meta_value;
   }

Change $json_data[$key->meta_key] to $json_data[$key->meta_key.$key->user_id] or simply change it to $json_data[$key->ummeta_id] $json_data[$key->meta_key]更改$json_data[$key->meta_key] $json_data[$key->meta_key.$key->user_id]或将其更改为$json_data[$key->ummeta_id]

your $key->meta_key is overwriting for every record. 您的$key->meta_key将为每条记录覆盖。 that's why only last record appeared. 这就是为什么只有最后一条记录出现的原因。 You don't actually need to loop through to get json data. 实际上,您不需要遍历即可获取json数据。

public function userlist()
{
   header('Content-Type: application/json; charset=utf-8');
   $query = $this->db_usermeta->userslist();
   $json_data = array(array());
   $user_id_map = array();
   $index = 0;
   foreach ($query as $key)
   {
        if(!isset($user_id_map[$key->user_id])){
            $user_id_map[$key->user_id] = $index++;
        }
        $currentIndex = $user_id_map[$key->user_id];
        $json_data[$currentIndex][$key->meta_key] = $key->meta_value;
   }
   echo json_encode($json_data);
}

just change your controller code to this and this will return json data. 只需将您的控制器代码更改为此,这将返回json数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM