如何在没有“喜欢”参数的情况下在codeiginiter中进行搜索?

时间:2016-05-14 02:26:51

标签: php html mysql database codeigniter

型号:

func == (lhs: Employee, rhs: Employee) -> Int {
    return (lhs.empName == rhs.empName && lhs.empCode == rhs.empCode)?1:0
}

控制器:

function search()
{
    $cari=$this->db->query("select * from uhd_user_order where user_order_reference  ");
    return $cari->result();;
}

查看:

function search_keyword()
{
    $this->input->GET('cari', TRUE);
    $beda['cari'] = $this->order_test_m->search();
    $this->load->view('admin/a',$beda);
}

我需要进行搜索,如果我只输入正确的代码,则会显示视图的结果。就像,如果我只搜索“s”,我的数据库中的数据将不会显示。

如何做到这一点?

1 个答案:

答案 0 :(得分:0)

我不确定你想要什么,我对你的代码有一些疑问,但我还不能发表评论。我必须猜测发生了什么:

我认为您的代码的工作原理是:每当“搜索”'单击按钮,将显示数据库中的所有用户名,对吧?

因为您的模型没有使用' get'要在数据库中搜索的数据

//Your function does not recieve the search string.
//This function uses $string to search in the 'users' table, to finds the name field == $string, and returns only 'name' row. 

function search($string){

    $query = $this->db->select('name')
        ->where('name',$string)
        ->get('users');

   //If the result is empty, return false, do nothing.
    if($query->num_rows() === 0){

        return false;
    }

    return $query->result();;
}

控制器

function search_keyword()
{
// Call search function with the input data.
// I think here will have some validation
//for example:
//if($this->form_validation->run() == false){
//       $this->load->view('errors/error');
//}
    $search_string = $this->input->GET('cari', TRUE); 
    $beda['cari'] = $this->order_test_m->search($search_string);
    $this->load->view('admin/a',$beda);
}

查看:

// This will ensure the ul will only display when $cari is not empty.
<?php if(count($cari)>0):?>
<ul>
<?php foreach($cari as $row):?>
   <li><?php echo $row->name;?></li>
<?php endforeach;?>
</ul>
<?php endif; ?>>