2013-08-23 2 views
1

이 코드는 다음과 같이 실행합니다. 데이터베이스의 모든 값을 표시하며 검색 할 때 검색합니다.하지만 예상 한 결과는 실행시 데이터를 표시하지 않아야합니다. 데이터베이스 및 검색 할 때만 결과를 표시해야합니다.검색하기 전에 db에서 값 가져 오기

컨트롤러 search1_site.php

<?php 
class Search1_site extends ci_controller 
{ 
    function index() 
    { 
     $data   = array(); 
     $keyword   = $this->input->post('keyword'); 
     $data['results'] = $this->search1_model->search($keyword); 
     $this->load->view('result_view', $data); 
    } 
} 
?> 

모델 search_model.php

<?php 
class Search_model extends CI_Model 
{ 
    function search($keyword) 
    { 
     $this->db->like('course_code', $keyword); 
     $query = $this->db->get('coursemaster'); 
     return $query->result(); 
    } 
} 
?> 

보기 result_view.php 페이지가 컨트롤러에 다음 줄에 키워드를로드

<form action="<?php echo site_url('search1_site/index');?>" method = "post"> 
<input type="text" name = "keyword" /> 
<input type="submit" id="opn" value = "Search" /> 
</form> 

<table> 
<tr> 
<th>course_code</th> 
<th>course name</th> 
</tr> 
<?php 
foreach ($results as $row) { 
?> 
<tr> 
<td><?php echo $row->course_code;?></td> 
<td><?php echo $row->course_name;?></td> 
</tr> 
<?php 
} 
?> 
</table> 
+0

난 정말 당신의 코드를 좋은 볼 않았지만, 일반적으로, 당신이 원하는 검색을 수행 할 데이터베이스, 데이터베이스는 PHP 및 배열보다 훨씬 빠릅니다. – Martijn

답변

1

:

보기
class Search1_site extends ci_controller 
{ 
    function index() 
    { 
     $data   = array(); 
     $keyword   = $this->input->post('keyword'); 
     if($keyword!=""){ 
      $data['results'] = $this->search1_model->search($keyword); 
     } 
     $this->load->view('result_view', $data); 
    } 
} 

:

<?php 
if($results){ 
foreach ($results as $row) { 
?> 
<tr> 
    <td><?php echo $row->course_code;?></td> 
    <td><?php echo $row->course_name;?></td> 
</tr> 
<?php 
} 
}else{ 
    //Display somthing 
} 
?> 
0

비어있는 상태가되어 쿼리가 모든 레코드를 검색하면 %%와 (과) 비슷할 수 있습니다.

$keyword = $this->input->post('keyword'); 

먼저 $ keyword가 비어 있지 않은지 확인하십시오. 비어 있지 않으면 데이터를 검색하십시오.

+0

나는 그것을 실행할 때 내 예상 결과를 검색하고 있습니다. 텍스트 상자와 제출 버튼을 표시하고 DB 결과를 표시하지 말아야합니다. @Rahul Tailwal – moses

+0

예. 이해합니다. 페이지로드시 값을 게시하지 않으므로 $ this-> input-> post ('keyword')는 공백이됩니다. 검색어가 '% $ keyword %'와 (과) 같은 열에서 select * 인 경우 모든 검색어가 검색됩니다. 그리고 $ 키워드가 비어 있음을 알 수 있습니다. 따라서 컨트롤러에 조건을 넣어 키워드가 게시되었는지 확인하십시오. –

+0

나는 당신에게 자신을 코딩 할 수있는 논리를 주려고 노력했다. 하지만 나는 다른 사람들이 당신을 위해 코드하기를 원한다고 생각합니다. –

0

무언가 같은 : 컨트롤러에서

if (isset($_POST['keyword'])) { ... } 
+0

너무 공격적이지 마 :) –

관련 문제