如何在codeigniter中使用搜索表单

时间:2014-07-07 05:54:35

标签: php mysql codeigniter

我有两个表“cv”和“users”。我希望我的搜索表单使用users-table中的'first_name'字段和cv-table中的'specialization'字段来过滤数据库。请帮我解决这个问题。在我的模型和&视图。感谢。

这是我的控制器

function search_worker()
{
    $data['query']=$this->kint_model->search_workers($this->input->post('search'));
    $this->load->view('hire_display',$data);
}

这是我的模特

function search_workers($search)
{
    //get user details from users table
    $this->db->select('*');
    $this->db->from('cv');
    $this->db->join('users', 'cv.profile_id = users.id');

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

    if($query->num_rows() > 0)
    {
        return $query->result();
    }

    //search for workers in hire_display view
    $query = $this->db->get_where('cv', array('first_name '=> $search))->result();

    //return $query for both queries
    return $query;
}

我对搜索表单的看法

<?php echo form_open('kint/search_worker');

    $data = array('first_name'=>'search', 'id'=>'search');
    echo form_input($data);

    $options = array(''  => 'Select specialization', 'database' => 'database', 'webdesign'=> 'webdesign', 'networking' => 'networking');
    echo form_dropdown('specialization', $options, $this->input-post('specialization'));

    $data = array('name'=>'submit', 'id'=>'submit', 'value'=>'Search Workers');
    echo form_submit($data);
?>

2 个答案:

答案 0 :(得分:0)

我相信&#34; first_name&#34;字段在&#34;用户&#34;表和&#34;专业化&#34;字段在&#34; cv&#34;表

$search_first_name = trim($this->input->post('first_name'));
$search_specialization = trim($this->input->post('specialization'));

$where_condition = "u.first_name LIKE '%$search_first_name%' ";

if(!empty($search_specialization)){
    $where_condition .= " OR c.specialization LIKE '%$search_specialization%' ";
}

$this->db->select('*');
$this->db->from("users u");
$this->db->join('cv c', 'u.id = c.profile_id');
$this->db->where($where_condition);

你的观点&#34;会是

echo form_open('kint/search_worker');

echo form_input(array('name' => 'first_name', 'value' => set_value('first_name')));

$options = array(''  => 'Select specialization', 'database' => 'database', 'webdesign'=> 'webdesign', 'networking' => 'networking');
echo form_dropdown('specialization', $options, set_value('specialization'));

</form>

答案 1 :(得分:0)

考虑cv表中的特化和Users表中的first_name

您可以根据自己的要求进行查询,也可以按顺序获取名字

if($this->input->post('search'))
{
$search = $this->input->post('search');            
$this->db->select('*');  // mention your field names you want to fetch
$this->db->from("users u");
$this->db->join('cv c', 'u.id = c.profile_id');
$this->db->where("u.first_name LIKE '%$search%' OR c.specialization LIKE '%$search%'");
$this->db->order('u.first_name', 'asc');   // asc or desc
$query = $this->db->get();
}