[英]codeigniter select result array
我正在嘗試從Codeigniter框架中的數據庫中選擇數據。
我的控制器
public function GetNewestPointers()
{
$user_last_login= $this->api_model->getUserLastLogin();
$result = $this->api_model->getNewestPointers($user_last_login);
if ($result)
{
$status = "1";
$msg = "SUCCESS";
}
else
{
$status = "0";
$msg = "ZERO_RESULT";
}
$output = array(
'status' => $status,
'message' => $msg,
'result' => $result
);
jsonOutput($output);
}
我的模特
public function getNewestPointers($user_last_login)
{
$query = $this->db->query("select * from `pointer_info` where `create_date`> '$user_last_login'");
return $query->result_array();
}
public function getUserLastLogin()
{
$userId = $this->input->get_post('userId');
$query = $this->db->query("select * from `users` where `id` = '$userId'");
if ($query->num_rows() == 0)
return "0";
else
return $query->row('last_login');
}
我當前的流程輸出是
"status":"1",
"message":"SUCCESS",
"result":[
{
"id":"15",
"title":"Test Pointer Title",
"upload_by":"53",
"create_date":"1413901780",
"current_status":"approved"
},
{
"id":"14",
"title":"Test Pointer Title",
"upload_by":"53",
"create_date":"1413901750",
"current_status":"approved"
}
]
但是我不需要使用上面的結果“ id”:“ 14”從另一個表中選擇數據
並且需要在“ item_list”中顯示第二個查詢數據,例如
"status":"1",
"message":"SUCCESS",
"result":[
{
"id":"15",
"title":"Test Pointer Title",
"upload_by":"53",
"create_date":"1413901780",
"current_status":"approved",
"item_list":[
{
"id":"138",
"name":"test",
"picture":"",
"purchase_status":"no",
"total_quantity":"25"
},
{
"id":"139",
"name":"New name2",
"picture":"",
"purchase_status":"no",
"total_quantity":"1"
}
]
}
]
這是我的示例代碼,希望對您有所幫助。
public function GetNewestPointers()
{
$user_last_login= $this->api_model->getUserLastLogin();
$result = $this->api_model->getNewestPointers($user_last_login);
if ($result)
{
$status = "1";
$msg = "SUCCESS";
}
else
{
$status = "0";
$msg = "ZERO_RESULT";
}
// sample code
foreach ($result as $data)
{
$item_list = $this->api_model->getIdData($data['id']);
$result[$data['id']]['item_list'] = $item_list;
}
$output = array(
'status' => $status,
'message' => $msg,
'result' => $result
);
jsonOutput($output);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.