codeigniter选择结果数组

时间:2014-10-21 15:45:54

标签: php mysql codeigniter

我正在尝试从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"
                }
            ]
    }
] 

1 个答案:

答案 0 :(得分:0)

这是我的示例代码,我希望它有所帮助。

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