21
2018
11

php做搜索功能

今天遇到一个问题:在做“搜索”功能时,输入查询条件后查询不了。

我做的是首页显示数据表package中的内容,但是有个条件,显示在首页的内容还必须是 :字段status=0,且printing=0的数据才能在首页列表中显示出来。

页面上有一个“搜索”功能,输入条件后就会根据条件来进行查询。

一般的搜索的话,只要在首页显示列表方法index()中给一个:

$map=array();//初始化查询条件

$map=$this->_search();//调用查询方法

$total = $this->Model->where ($map)->count(); //这个主要是用来计算页面显示数据条数的

if ($total == 0) {
$_list = '';
} else {
$_list = $this->Model->where ($map)->limit( $post_data ['first'] . ',' . $post_data ['rows'] )->select();
}

然后,就是写一个_search():

如:

protected function _search(){

$map = array ();
$post_data = I ( 'post.' );

if ($post_data ['packageid'] != '') {
$map ['packageid'] = array (
'like',
'%' . $post_data ['packageid'] . '%' 
);
}

return $map;

}

最后,在设置的“搜索”菜单中,调用这个搜索方法。

但是,我做的这个,搜索的同时,还要确保在字段status=0,且printing=0的数据中进行搜索。

我一直在想这个限制条件该加在什么地方。各种尝试和查询后,才知道。限制条件直接加在SQL语句中就行了(如下红色的地方)。(我自己试的时候一直在如下蓝色的地方加条件,屡试屡败!)

$map=array();
$map=$this->_search();

$total = $this->Model->where ($map)->where(array('status' =>0,'print_status'=>0))->count();

if ($total == 0) {
$_list = '';
} else {
$_list = $this->Model->where ($map)->where(array('status' =>0,'print_status'=>0))->limit( $post_data ['first'] . ',' . $post_data ['rows'] )->select();
}

原文链接:https://www.qiquanji.com/post/7800.html

本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。

微信扫码关注

更新实时通知

« 上一篇 下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。