Self-developed paging class, applicable to various scenarios that use native SQL-PHP source code

Source: Internet
Author: User
The self-developed paging class can be used in various scenarios where native SQL is used to jump to [1] [2] [full screen preview]
 Totle_number = $ totle_number; $ this-> per_page_number = $ per_page_number; $ this-> totle_pages = $ totle_number = 0? 1: ($ totle_number % $ per_page_number = 0 )? Floor ($ totle_number/$ per_page_number): floor ($ totle_number/$ per_page_number) + 1); $ this-> current_page = $ current_page> $ this-> totle_pages? $ This-> totle_pages: $ current_page <= 0? 1: $ current_page;}/*** get the current page ** @ return integer current page number */public function getCurrentPage () {return $ this-> current_page ;} /*** get the total number of pages ** @ return integer total number of pages */public function getTotlePages () {return $ this-> totle_pages ;} /*** obtain where the current query should start ** @ return integer where the current query should start */public function getCurrentNum () {return ($ this-> current_page-1) * $ this-> per_page_number;}/*** obtain the number of pages per page ** @ retu Rn integer volume per page */public function getPerPageNum () {return $ this-> per_page_number ;} /*** get the page number displayed on the foreground ** @ param integer $ max_show_page_number the maximum page number displayed, the default value is 5 * @ return Array, which contains the number of pages */public function getPages ($ max_show_page_number = 5) {if ($ max_show_page_number> $ this-> totle_pages) {return $ this-> createArray (1, $ this-> totle_pages);} $ half = floor ($ max_show_page_number/2); if ($ this-> current_pa Ge <= floor ($ this-> totle_pages/2) {if ($ this-> current_page <$ half) {return $ this-> createArray (1, $ max_show_page_number);} else {return $ this-> createArray ($ this-> current_page-$ half + 1, $ this-> current_page-$ half + $ max_show_page_number );}} else {if ($ this-> totle_pages-$ this-> current_page <$ half) {return $ this-> createArray ($ this-> totle_pages-$ max_show_page_number, $ this-> totle_pages-1) ;} Else {return $ this-> createArray ($ this-> current_page + $ half-$ max_show_page_number, $ this-> current_page + $ half-1 );}}} /*** create an array, the value of the array starts from $ start to $ end ** @ param integer $ start * @ param integer $ end the value of the array * @ param integer $ step the value of the array increases step, the default value is 1 * @ return Array. if the step size is 0, an empty Array is returned. if the step size is greater than 0, a value sorted from small to large is returned, otherwise, return the value */private function createArray ($ start, $ end, $ step = 1) {if ($ step = 0) {return [];} $ Result = []; for ($ I = 0; $ I <= ($ end-$ start); $ I + = $ step) {$ result [$ I] = $ start + $ I;} return $ result ;}}?>

2.[Code][PHP] codeJump to [1] [2] [full screen preview]

 GetCount (); // we suppose there is a model can do SQL query // Obtain the current page number, get $ current_page = isset ($ _ GET ['Page']) from the get parameter. $ _ GET ['Page']: 1; // instantiate the page class $ page = new page ($ count, $ current_page); // query the information to be displayed on the current Page, use the SQL LIMIT $ data = $ model-> query ()-> limit ($ page-> getCurrentNum (), $ page-> getPerPageNum ()); // Now you can use a series of get methods to obtain the information you need to display on the page. // Set $ data, $ current_page, and $ page-> getPages () pass it to the view. if you want to show it, it depends on your wishes. // $ Page-> getPages () can automatically obtain the pages near the current page number and return an array, which can be displayed directly at the front-end.?>

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.