2011-09-18 4 views
1

두 권의 책과 저자가 있습니다. CodeIgniter를 사용하여 사용자가 결과를 필터링 할 수있게하고 싶습니다. 예를 들어 author_id = 'x', book_lang = 'y'및 publication_year = '2000'인 경우 결과를 표시 할 수 있습니다. 필터 기준 값은 드롭 다운을 통해 사용자가 입력합니다.CodeIgniter로 결과 필터링

codeigniter 도우미 및 바인딩 등을 사용하여 쿼리를 생성하는 가장 좋은 방법은 무엇입니까?

예를 들어, 아래 쿼리는 author_id = 1 인 모든 것을 제공합니다. author_id = 1; language = 'all'(언어에 where 절을 넣지 않음) 및 발행 연도 = 'all'또한 where 절을 넣지 않습니다. 수동으로 값과 코드를 확인해야합니까 아니면 허용 된 codigniter 도우미 메서드가 있습니까? 드롭 다운 값이 '모두 표시'인 경우 where 절에서 제거 할 필터?

$sql = "select * from AUTHORs a , BOOKS b where 
     a.AUTH_ID = b.BOOK_AUTH_ID 
     and b.BOOK_AUTH_ID = ? 
     and b.BOOK_LANGUAGE = ? 
     and b.PUB_YEAR = ? "; 

$query = $this->db->query($sql, array ($author_id, $book_lang, $pub_year)); 

답변

3
당신은 당신의 쿼리를 생성하기 위해 CodeIgniter의의 액티브 레코드 클래스를 사용해야합니다

다음 문서를 참조

$this->db->select('*'); 
$this->db->from('AUTHORs a , BOOKS b'); 
$this->db->where('a.AUTH_ID', 'b.BOOK_AUTH_ID'); 

if ($author_id != 'all') $this->db->where('b.BOOK_AUTH_ID', $author_id); 
if ($book_lang != 'all') $this->db->where('b.BOOK_LANGUAGE', $book_lang); 
if ($pub_year != 'all') $this->db->where('b.PUB_YEAR', $pub_year); 

$query = $this->db->get(); 

이 CodeIgniter의의 액티브 레코드 클래스에 대한 자세한 내용을 읽으려면 :
http://codeigniter.com/user_guide/database/active_record.html