当前位置:学者斋 >

计算机 >php语言 >

PHP实现搜索查询功能的方法技巧

PHP实现搜索查询功能的方法技巧

像这样简单的语句select*from table where name like '%$_POST["content"]%';虽然可以实现搜索功能但是却不能够真正应到项目中,为什么呢?下面,小编为大家搜索整理了PHP实现搜索查询功能的方法技巧,希望能给大家带来帮助!更多精彩内容请及时关注我们应届毕业生考试网!

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();

}


标签: PHP
  • 文章版权属于文章作者所有,转载请注明 https://xuezhezhai.com/jsj/php/0lmo23.html