简体   繁体   中英

codeigniter select result array

I am trying to select data from my database in codeigniter framework.

My controller

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

My Model

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

My current process output is

"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"
    }
]

But I have need no select data from another table using above result "id":"14"

and need to show second query data in "item_list" like

"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"
                }
            ]
    }
] 

This is my sample code, I hope it helps.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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