PHP search and query function implementation, php search and query function

Source: Internet
Author: User

PHP search and query function implementation, php search and query function

Today, I encountered a problem: when I used the "Search" function, I couldn't query it after I entered the query conditions.

What I am doing is to display the content in the package of the data table on the homepage, but there is a condition that the content displayed on the homepage must also be: field status = 0, data with printing = 0 can be displayed on the homepage list.

There is a "Search" function on the page. after entering the conditions, the search will be performed according to the conditions.

For a general search, you only need to give:

$ Map = array (); // initialize the query condition $ map = $ this-> _ search (); // call the query method $ total = $ this-> Model-> where ($ map)-> count (); // This is mainly used to calculate the if ($ total = 0) {$ _ list = '';} of the number of data entries displayed on the page '';} else {$ _ list = $ this-> Model-> where ($ map)-> limit ($ post_data ['first']. ','. $ post_data ['rows '])-> select ();}

Then, write a _ search ():

For example:

protected function _search(){$map = array ();$post_data = I ( 'post.' );if ($post_data ['packageid'] != '') {$map ['packageid'] = array ('like','%' . $post_data ['packageid'] . '%' );}return $map;}

Finally, in the Set "Search" menu, call this search method.

However, when searching, make sure that the data in the field status = 0 and printing = 0 is searched.

I 've been wondering where to add this restriction. After trying and querying, you will know. The restrictions can be added directly to the SQL statement (the following is red ). (When I tried it myself, I kept adding conditions in the blue area below, so I tried and tried again and again !)

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

The above section describes how to implement the PHP search and query function. I hope it will be helpful to you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.