2014-02-25 46 views
0

控制器:如何設置LIKE子句CI

function search() 
    { 
     $this->load->model('membership_model'); 
     $query = $this->membership_model->search(); 
     var_dump($query->result());} 

型號:

function search() 
    { 
    $match = $this->input->post('Search'); 
    $this->db->like('title',$match); 
    $q = $this->db->get('feeds'); 
    return $q; 
    } 

我想從我的數據庫中選取標題包含$匹配所有行,但它返回我的所有行。我有一個輸入表單,我插入單詞。在模型中,我希望在每個標題中搜索單詞,並僅返回那些包含來自我的輸入表單的單詞的標題。 形式:

echo anchor('site/search','Search')."<br/><br/>"; 
    echo form_input('search','search'); 
+0

你要去的URL是什麼?你確定'Search'正確地被張貼?你如何提交你的表格? –

+0

檢查模型中您上次執行的查詢。 –

+0

我只在模型中放入第三行,它將所有行都返回給我。這意味着像子句不起作用回聲錨('site/search','搜索')。「

」; echo form_input('search');這是我的形式 – user3313651

回答

0

我實在無法理解的問題,但我認爲這個代碼是你所需要的

型號:

function search($match){ 
    $this->db->like('title',$match); 
    $q = $this->db->get('feeds'); 
    return $q->result(); 
} 

控制器:

function search_title() 
    { 
     $this->load->model('membership_model'); 
     $search_value = $this->input->post('search'); 
     $this->data['title'] = $this->membership_model->search($search_value); 
    } 

查看:

<table> 
    <?php foreach($title as $value) : ?> 
    <tr> 
     <td> <?=$value->nameofcolumn; ?> </tr> //change the word nameofcolumn to the name of the column in your database what column you want to fetch 
    </tr> 
    <?php endforeach; ?> 
    </table>