如何使用两个字段在CodeIgniter中形成搜索

时间:2014-06-19 17:39:29

标签: codeigniter

我想在Codeigniter中使用两个字段创建搜索表单。 但我对制作控制器和模型很困惑。

查看:

echo form_open("home/pencarian","class='form-inline navbar-search'");   

<input id="srchFld" class="srchTxt" name="keyword" type="text" value=" echo $this->session->userdata("keyword");" />
<select class="srchTxt" name="kategori">
    <option>All</option>
    foreach ($kat as $kategori){
        $id = $kategori['id'];
        $nama_kategori = $kategori['nama_kategori'];
        $created_at = $kategori['created_at'];
        <option value=" echo $id;  ?>"><?php echo $nama_kategori; </option>
    } 
</select> 

echo form_submit('action', 'Cari', "class='btn btn-primary'");
echo form_close(); 

如何制作控制器和型号?

1 个答案:

答案 0 :(得分:0)

模型example_model.php

public function search($search_txt, $search_category) {
    $this->load->database();
    $sql = "select * from search_table where search_field = ? and search_category = ?;";
    $query = $this->db->query($sql, array($search_txt, $search_category));
    return $query->result();
}

控制器主页

public function pencarian() {
    $this->load->model('example_model', 'm');
    $search_txt = $this->input->post('srchFld');
    $search_category = $this->input->post('kategori');
    $data['search_result'] = $this->m->search($search_txt, $search_category);
    $this->load->view('example_view', $data);
}

查看example_view

<?php foreach($search_result as $r): ?>
<div><?php echo $r->field1 . ' ' . $r->field2; ?></div>
<?php endforeach; ?>