2012-02-29 4 views
0
내가 CodeIgniter의 나머지 모듈을 테스트하고 있습니다

는 여기 내 간단한 GET 기능입니다 :CodeIgniter의 쿼리

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

그것은 지금까지 작동합니다,하지만 난 모르겠어요 이유를 '나는 결과로 아이디 (3)를 받고 있어요 http://.../test/id/2을 열 때 나는 http://.../test/id/1

<xml><id>2</id><attribut1>Testdata</attribut1><attribut2>asdfasdf</attribut2><testcol>asf</testcol></xml> 

를 열 때 결과로 ID 2를 받고 있어요.

이럴 필요가 없습니다. http://.../test/id/1 -> id 1?

답변

1

이것은 하나의 문제입니다. $users 배열 (0부터 시작 함)을 인덱싱하지만 ID는 1부터 시작합니다. 사용자 ID에 갭이있을 때 더 나쁜 문제가 발생할 것입니다 (단 1 개가 아닌 임의의 간격으로 꺼짐). 대신 다음을 시도하십시오 :

function test_get() 
{ 
    if(!$this->get('id')) 
    { 
     $this->response(NULL, 400); 
    } 

    $user = $this->db->where('id', $this->get('id'))->get('test')->first_row(); 

    if($user) 
    { 
     $this->response($user, 200); // 200 being the HTTP response code 
    } 

    else 
    { 
     $this->response(array('error' => 'User could not be found'), 404); 
    } 
} 
+0

물론 배열은 0입니다. 귀하의 예가 해결책입니다. 고마워요 :) – milepile