public function lists() { header("Access-Control-Allow-Origin: *"); header("Content-Type: application/json; charset=UTF-8"); $Item_model = new Item_model(); $result = $Item_model->get_all_items(); if ($result === FALSE) { print json_encode(array('status' => 'failed')); } else { print json_encode($result); } }
public function test_get_all_items() { $expected = array(1 => "Samsung", 2 => "HTC", 3 => "Iphone"); $Item_model = new Item_model(); $lists = $Item_model->get_all_items(); $i = 0; foreach ($lists as $item) { $this->assertEquals($expected[$item->id], $item->name); $i++; if ($i === 3) { break; } } }
public function lists_get() { $Item_model = new Item_model(); $lists = $Item_model->get_all_items(); if ($lists === FALSE) { print json_encode(array('status' => 'failed')); } $id = $this->get('id'); // If the id parameter doesn't exist return all the users if ($id === NULL) { // Check if the users data store contains users (in case the database result returns NULL) if ($lists) { // Set the response and exit $this->response($lists, REST_Controller::HTTP_OK); // OK (200) being the HTTP response code } else { // Set the response and exit $this->response(['status' => FALSE, 'message' => 'No users were found'], REST_Controller::HTTP_NOT_FOUND); // NOT_FOUND (404) being the HTTP response code } } // Find and return a single record for a particular user. $id = (int) $id; // Validate the id. if ($id <= 0) { // Invalid id, set the response and exit. $this->response(NULL, REST_Controller::HTTP_BAD_REQUEST); // BAD_REQUEST (400) being the HTTP response code } // Get the user from the array, using the id as key for retreival. // Usually a model is to be used for this. $list = NULL; if (!empty($lists)) { foreach ($lists as $key => $value) { $value = (array) $value; $value['id'] = (int) $value['id']; if (isset($value['id']) && $value['id'] === $id) { $list = $value; } } } if (!empty($list)) { $this->set_response($list, REST_Controller::HTTP_OK); // OK (200) being the HTTP response code } else { $this->set_response(['status' => FALSE, 'message' => 'Item could not be found'], REST_Controller::HTTP_NOT_FOUND); // NOT_FOUND (404) being the HTTP response code } }