Php search and query Functions

Source: Internet
Author: User

Php search and query Functions

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 query Conditions

$ Map = $ this-> _ search (); // call the query method

$ Total = $ this-> Model-> where ($ map)-> count (); // this is mainly used to calculate the number of data entries displayed on the page.

If ($ total = 0 ){
$ _ List = '';
} 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 ();
}

I would like to share with you.

For more information, see.

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.