Thinkphp Method for retaining conditional pages on homepage pages and search pages during search _ php instance

Source: Internet
Author: User
This article mainly introduces how to retain the conditional Paging for the home page and search page during Thinkphp search. Two different methods are used to analyze in detail how to maintain the search condition when searching by page, it is a very useful technique. If you need it, you can refer to the examples in this article to describe Thinkphp's method of retaining conditional pages on the homepage and search pages during search. Share it with you for your reference. The specific implementation method is as follows:

When performing a search query, I suddenly found that the paging code used on the homepage encountered an error while searching for the page. The paging code on the homepage (the start and end sections in the Code are paging codes)

The Code is as follows:

Public function index (){
$ Res = D ('info'); // instantiate a Data Object

/*********** Start ************/
Import ('org. Util. page'); // import the paging class
$ Count = $ res-> count (); // query the total number of records meeting the requirements
$ Page = new Page ($ count, 3); // instantiate the total number of records imported by the paging class (another parameter is the number of custom pages)
// $ Page-> rollPage = 3; // by default, the Page displays 5 pages that can be modified.
$ Show = $ Page-> show (); // display the output by Page
// Query paging data
$ List = $ res-> order ('iid desc')-> limit ($ Page-> firstRow. ','. $ Page-> listRows)-> select ();
/*********** End ************/

$ This-> assign ('LIST', $ list); // assign a value to a dataset

/********* Start *************/
$ This-> assign ('page', $ show); // value-assigned paging output
/********** End *************/

$ This-> display (); // output Template
}


Search Code (the part marked between start and end in the Code is the paging code, and the comments mark the query conditions saved during paging jump ), either of the following methods can save the conditions (it is not clear whether this is the write Specification) and query:

The Code is as follows:

Public function search (){
$ Res = D ('info ');
$ Name = $ _ REQUEST ['name'];
$ Sear ['name'] = array ('like', '%'. $ name. '% ');

/********* Start *************/
Import ('org. Util. page'); // import the paging class
$ Count = $ res-> where ($ sear)-> count (); // query the number of data entries
$ Page = new Page ($ count, 2); // instantiate the paging Function
/********** End *************/

// Save the query conditions when redirecting by page.
Foreach ($ sear as $ key => $ val ){
$ Page-> parameter. = "$ key =". urlencode ($ name). "&"; // assign a value to Page
}

/********* Start *************/
$ Show = $ Page-> show (); // display the output by Page
// Query paging data
$ Val = $ res-> where ($ sear)-> limit ($ Page-> firstRow. ','. $ Page-> listRows)-> select ();
/********** End *************/

$ This-> assign ('search', $ val );

/********* Start *************/
$ This-> assign ('page', $ show );
/********** End *************/

$ This-> display ();
}


Note:

The Code is as follows:

Foreach ($ sear as $ key => $ val ){
$ Page-> parameter. = "$ key =". urlencode ($ name). "&"; // assign a value to Page
}
"$ Key =". urlencode ($ name )."&";

$ Name corresponds to $ name = $ _ REQUEST ['name.

Second:

The Code is as follows:

Public function search (){
$ Res = D ('info ');
$ Name = $ _ REQUEST ['name'];
$ Sear ['name'] = array ('like', '%'. $ name. '% ');
Import ('org. Util. page'); // import the paging class
$ Count = $ res-> where ($ sear)-> count (); // query the number of data entries
$ Page = new Page ($ count, 2); // instantiate the paging Function
// Ensure the query conditions during paging jump
Foreach ($ sear as $ key => $ val ){
$ Page-> parameter. = "$ key =". urlencode ($ val [1]). '&';
}
$ Show = $ Page-> show (); // display the output by Page
// Query paging data
$ Val = $ res-> where ($ sear)-> limit ($ Page-> firstRow. ','. $ Page-> listRows)-> select ();
$ This-> assign ('search', $ val );
$ This-> assign ('page', $ show );
$ This-> display ();
}


$ Val [1] is used because $ sear is an array, and $ val [1] corresponds to the condition I want to search, so that the condition can be kept paging.

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.