PHP Search Query Function implementation

Source: Internet
Author: User
This article mainly introduces the PHP search query function implementation code, very good, with reference value, the need for friends can refer to the next

Today we have a problem: when you do a "search" function, you cannot query after you enter the query criteria.

What I do is to display the contents of the datasheet package in the home page, but there is a condition that the content displayed on the front page must also be: Field status=0, and printing=0 data can be displayed in the first page list.

There is a "search" function on the page, and the criteria will be queried when the condition is entered.

General search words, as long as the first page display list Method index () to one:

$map =array ();//Initialize query condition $map= $this->_search ();//Call Query method $total = $this->model->where ($map)->count (); This is primarily used to calculate if ($total = = 0) of the number of data bars displayed on the page {$_list = ';} else {$_list = $this->model->where ($map)->limit ($post _data [' first ']. ',' . $post _data [' Rows '])->select ();}

Then, just write a _search ():

Such as:

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, I do this, while searching, also make sure to search in the field status=0, and printing=0 the data.

I've been thinking about where this restriction should be added. A variety of attempts and inquiries before they are known. The restrictions are added directly to the SQL statement (red places below). I have been in the following blue places to add conditions, try repeatedly defeated! )

$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)->whe Re (array (' status ' =>0, ' Print_status ' =>0))->limit ($post _data [' first ']. ',' . $post _data [' Rows '])->select ();}

The above is the whole content of this article, I hope that everyone's study has helped.


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.