The example of this article is about how to thinkphp the first page pagination and the search page to keep the condition paging when the search is implemented. Share to everyone for your reference. The implementation methods are as follows:
In doing a search query suddenly found in the page with the paging code in the search page when the use of error, home page code (Mark start and end part of the code is pagination code)
Copy Code code as follows:
Public Function index () {
$res =d (' Info ');//Instantiate data Object
/**********start************/
Import (' ORG. Util.page ');//Import pagination class
$count = $res->count ()//The total number of records that the query meets the requirements
$Page = new Page ($count, 3);//Instantiate paging class incoming total records (another parameter is the number of custom page bars)
$Page->rollpage = 3;//By default, the page displays 5 pages that can be modified
$show = $Page->show ()//Paging display output
Make Paging data query
$list = $res->order (' iid desc ')->limit ($Page->firstrow. '. $Page->listrows)->select ();
/**********end************/
$this->assign (' list ', $list);//Assignment Data set
/*********start*************/
$this->assign (' page ', $show);//Assignment Paging output
/*********end*************/
$this->display (); Output template
}
Search code (the part of the code that is marked between start and end is the paging code, the comment marks the page jump to save the query condition), the following two methods can save the condition (not clear this is not written specifications), query:
Copy Code code as follows:
Public Function Search () {
$res =d (' Info ');
$name =$_request[' name '];
$sear [' name '] = Array (' Like ', '% '. $name. ') %');
/*********start*************/
Import (' ORG. Util.page ');//Import pagination class
$count = $res->where ($sear)->count ()//query data bar count
$Page =new page ($count, 2);//Instantiate paging function
/*********end*************/
Save query conditions when paging jumps
foreach ($sear as $key => $val) {
$Page->parameter. = "$key =". UrlEncode ($name). &//Assign value to page
}
/*********start*************/
$show = $Page->show ()//Paging display output
Make Paging data query
$val = $res->where ($sear)-> $val = $res->where ($sear)->limit ($Page->firstrow. ', '. $Page->listrows) ->select ();
/*********end*************/
$this->assign (' Search ', $val);
/*********start*************/
$this->assign (' page ', $show);
/*********end*************/
$this->display ();
}
Note:
Copy Code code as follows:
foreach ($sear as $key => $val) {
$Page->parameter. = "$key =". UrlEncode ($name). &//Assign value to page
}
"$key =". UrlEncode ($name). & ";
The $name in this should be extracted $name =$_request[' name '; the value obtained.
The second type:
Copy Code code as follows:
Public Function Search () {
$res =d (' Info ');
$name =$_request[' name '];
$sear [' name '] = Array (' Like ', '% '. $name. ') %');
Import (' ORG. Util.page ');//Import pagination class
$count = $res->where ($sear)->count ()//query data bar count
$Page =new page ($count, 2);//Instantiate paging function
Ensure query conditions when paging jumps
foreach ($sear as $key => $val) {
$Page->parameter. = "$key =" UrlEncode ($val [1]). ' & ';
}
$show = $Page->show ()//Paging display output
Make Paging data query
$val = $res->where ($sear)->limit ($Page->firstrow. ', '. $Page->listrows)->select ();
$this->assign (' Search ', $val);
$this->assign (' page ', $show);
$this->display ();
}
Using $val[1] is because the $sear is an array, and $val[1] corresponds to the condition I'm looking for, so I can keep the condition paging.
More interested in thinkphp related content readers can view the site topics: "thinkphp Introductory Course" and "thinkphp Common methods Summary"
I hope this article will help you with the PHP program design based on thinkphp framework.