I’m testing the REST Module of CodeIgniter,
here is my simple GET Function:
function test_get()
{
if(!$this->get('id'))
{
$this->response(NULL, 400);
}
$query = $this->db->query('select * from test');
$users = $query->result();
$user = @$users[$this->get('id')];
if($user)
{
$this->response($user, 200); // 200 being the HTTP response code
}
else
{
$this->response(array('error' => 'User could not be found'), 404);
}
}
It works so far, but I’m not sure why I’m getting the id 2 as a result when I open http://…/test/id/1
<xml><id>2</id><attribut1>Testdata</attribut1><attribut2>asdfasdf</attribut2><testcol>asf</testcol></xml>
When I open http://…/test/id/2 I’m getting the the id 3 as a result.
Shouldn’t that http://…/test/id/1 -> id 1?
This is an off-by-one issue. You’re indexing the
$usersarray (which is zero-based), but your IDs are 1-based. You’ll have even worse issues when your user IDs have gaps (you’ll be off by random increments, rather than just 1). Try this instead: