Thinkphp paging code (conditional paging for homepage pages and search pages)

Source: Internet
Author: User
When performing a search query, I suddenly found that the paging code used on the home page encountered an error while searching the page. The paging code on the home page (green indicates the paging code) publicfunctionindex () {$ resD (& amp; #...

When performing a search query, I suddenly found that the paging code used on the homepage encountered an error while searching the page. The paging code on the homepage (green indicates the paging code)

Public function index () {$ res = D ('info'); // instantiate the Data object import ('org. util. page '); // Import 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 number of pages displayed on the Page is 5. you can modify $ show = $ Page-> show (); // Display output by Page // query paging Data $ list = $ res-> order ('iid desc ')-> limit ($ Page-> firstRow. ','. $ Page-> listRows)-> select (); $ this-> assign ('list', $ list); // assign a dataset $ this-> assign ('page ', $ show); // value-assigned paging output $ this-> display (); // output Template}

Search the code (green indicates the paging code, and red indicates the save condition). you can save the conditions in either of the following two methods (it is not clear if this is the write specification) and query:

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 // make sure that the query conditions are foreach ($ sear as $ key => $ val) {$ Page-> parameter. = "$ key = ". urlencode ($ name ). "&"; // value to Page} $ show = $ Page-> show (); // Display output by page // query data by page $ val = $ res-> where ($ sear)-> $ val = $ res-> where ($ sear) -> limit ($ Page-> firstRow. ','. $ Page-> listRows)-> select (); $ this-> assign ('search', $ val); $ this-> assign ('page', $ show ); $ this-> display ();}

Note:

Foreach ($ sear as $ key => $ val) {$ Page-> parameter. = "$ key = ". urlencode ($ name ). "&"; // assigned to Page} "$ key = ". urlencode ($ name ). "&"; $ name corresponds to $ name = $ _ REQUEST ['name.

Second:

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

$ 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.