2012-12-04 3 views
-1

기본적으로 사용자가 2 개의 변수 $ no 및 $ first를 입력 할 수있는 양식을 표시하지만 두 필드를 입력 할 때만 결과를 표시 할 수 있습니다. 나는 그것이 1 개의 변수를 입력 할 때 결과를 표시 할 것입니다.Codeigniter 검색 쿼리가 올바르게 작동하지 않습니다.

나는 어떤 종류의 OR 문을 사용할 수 있다고 생각했지만 구현 방법을 잘 모르겠습니다. 어떤 도움을 주시면 고맙겠습니다. 그리고 코드 명에 관해서는 상당히 새롭지는 않다면 사과드립니다.

컨트롤러

public function query() 

{ 

$no = $this->input->post('no'); 
$first = $this->input->post('first'); 
$this->load->model("test"); 
    $data['query']=$this->test->query($no,$first); 
    $this->load->view('query',$data); 

} 

모델

function query($no, $first) 
{ 

return $query = $this->db->get_where('table', array('no' => $no, 
'first' => $first))->result(); 

} 

보기

당신은 당신의 WHERE 인수 밖으로 분리 할 필요가
<?php foreach($query as $row): ?> 
<tr> 
<td><?php echo $row->no; ?></td> 
<td><?php echo $row->date; ?></td> 
<td><?php echo $row->first; ?></td> 


</tr> 
<?php endforeach; ?> 

답변

0

:

function query($no, $first) 
{ 
    $this->db->select(); 
    $this->db->from('table'); 
    $this->db->where('no', $no); 

    if (!empty($first)) 
    { 
     $this->db->or_where('first', $first); // or use `and_where` 
    } 
    return $query = $this->db->get()->result(); 
} 
,536을

출처 : http://ellislab.com/codeigniter/user-guide/database/active_record.html

+0

이것은 no가 $ no와 같을 때만 작동합니다. 그 질문이 그가 필요하거나 필요로하는 것을 읽었습니다. –

0
function query($no = '%', $first = '%') 
{ 
$this->db->where("'no' = $no OR 'first' = $first"); 
$query = $this->db->get('table'); 
if($query->num_rows() >= 0) 
{ 
    $return $query->result(); 
} 
} 

당신은 값을 기본 값으로 와일드 카드를 공급하는 경우 중 하나 그럼 그냥 큰 따옴표 안에 전체 곳에 문을 작성 (사실 모두가 모든 결과를 반환 할 비어있을 수 있음) 비어있을 수 .

문자열이 비어있는 경우 모든 결과를 반환하지 않으려면 와일드 카드를 제공 할 필요가 없지만 빈 결과가 나오거나 반환되지 않는 경우 일부 검사를 수행해야합니다. 당신은 내가 당신이 원하는 아이디어를 얻을

function query($no = "", $first = "") 
{ 
$response = array(); 
if($first != "" || $no != ""){ 
    $where = ""; 
    if($no == "") $where = 'first = '.$first; 
    if($first == "") $where = 'no = '.$no; 
    if($first != "" && $no != "") $where = 'no = '.$no.' AND first = '.$first; 
    $fetch = $this->db->where($where)->from('table')->get(); 
    if($fetch->num_rows() > 0) $response = $fetch->result_array(); 
} 
return $response; 
} 

희망 먼저 where 절을 준비 시도 할 수

관련 문제