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.