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

如题所述

第1个回答  2022-07-28

  下面是首页显示数据表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();

  }


更多相关文章推荐:

相似回答