Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/251.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 当输入值为空时,代码点火器搜索返回表中的所有记录_Php_Sql_Codeigniter_Post - Fatal编程技术网

Php 当输入值为空时,代码点火器搜索返回表中的所有记录

Php 当输入值为空时,代码点火器搜索返回表中的所有记录,php,sql,codeigniter,post,Php,Sql,Codeigniter,Post,当关键字输入为空时,搜索结果将显示所有记录,如何显示显示“未找到搜索词”的消息 如果我还想添加另一个输入值,比如input->post('date'),该怎么办 控制器: $keywords = $this->input->post('keywords'); $data['results'] = $this->search_model->get_results($keywords ); $this->template->show('results',$dat

当关键字输入为空时,搜索结果将显示所有记录,如何显示显示“未找到搜索词”的消息

如果我还想添加另一个输入值,比如input->post('date'),该怎么办

控制器:

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

$data['results'] = $this->search_model->get_results($keywords );
$this->template->show('results',$data);
型号:

function get_results($keywords = 'default'){

       $query = $this->db->select('*')->from('list')
       ->where("name LIKE '%$keywords%'")->get();

       return $query->result_array();
    }

尝试使用codeigniter
$this->db->like
方法。 如果您的
$keyward
为空,请尝试以下操作

$keywords = $this->input->post('keywords');
if(!empty($keywords)){
   $data['results'] = $this->search_model->get_results($keywords ); 
}else{
$data['result']="not found";
}
$this->template->show('results',$data);

function get_results($keywords = null){

   if(!$keywords){
   return false;
   }
   $query = $this->db->select('*')->from('list')->like("name",$keywords)->get();
   return $query->result_array();
}
每次查询运行并返回数据时,因为您传递了$keyword='default',所以如果
$keyword
为空,则使用default

尝试使用codeigniter
$this->db->like
方法。 如果您的
$keyward
为空,请尝试以下操作

$keywords = $this->input->post('keywords');
if(!empty($keywords)){
   $data['results'] = $this->search_model->get_results($keywords ); 
}else{
$data['result']="not found";
}
$this->template->show('results',$data);

function get_results($keywords = null){

   if(!$keywords){
   return false;
   }
   $query = $this->db->select('*')->from('list')->like("name",$keywords)->get();
   return $query->result_array();
}
每次查询运行并返回数据时,因为您传递了$keyword='default',所以如果
$keyword
为空,则使用default
这是因为你有一个相似的说法

所以如果你写

从名为“%”的列表中选择*

它将返回所有


若你们有一个空关键字,你们最好不要去数据库,因为你们并没有任何东西可以搜索。

这是因为你们有一个like语句

所以如果你写

从名为“%”的列表中选择*

它将返回所有

若你们有一个空关键字,你们最好不要去数据库,因为你们并没有任何东西可以搜索