簡體   English   中英

在Codeigniter中生成JSON查詢結果

[英]Generating JSON Query Result in Codeigniter

所以我有一個名為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   |

我嘗試使用Controller和Model使用Codeigniter(v 3.1.9)從該數據庫生成所有數據的json數據。

這是我的模型 (模型名稱:db_usermeta)

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

這是我的控制器

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);
}

當我使用瀏覽器打開瀏覽器以使用Web Developer工具檢查json數據時,結果僅顯示最后一條記錄,在這種情況下,僅顯示user_id 2中的數據,如下所示:

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

我想要實現的是顯示嵌套的所有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"
  }
]

我怎樣才能做到這一點? 我在控制器和模型上犯了錯誤嗎? 非常感謝您的幫助。

由於兩個記錄的元密鑰fullname都相同,因此您需要將密鑰名更改為唯一的名稱

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

$json_data[$key->meta_key]更改$json_data[$key->meta_key] $json_data[$key->meta_key.$key->user_id]或將其更改為$json_data[$key->ummeta_id]

您的$key->meta_key將為每條記錄覆蓋。 這就是為什么只有最后一條記錄出現的原因。 實際上,您不需要遍歷即可獲取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);
}

只需將您的控制器代碼更改為此,這將返回json數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM