This article mainly introduces the PHP back-end method to achieve page sub-page subscript generation code, has a certain reference value, now share to everyone, the need for friends can refer to
The page's paging selection effect directly affects the user experience. There are many ways to do this, and the main advantage of the method I write here is that the front-end separation allows you to define the length and number of pages in the page.
Test legend:
:
Implementation code:
/** * * @param $page page number (1 to infinity) * @param $num the number of rows in the data * @param Total row count of $rows data * @param The maximum length of the subscript * @return Array */Public function pagedate ($page, $num, $rows, $length) {//Initialize data $MaxPage = 0; Maximum page number $MinPage = 0; Minimum page number $ServerPage = 0; The page number displayed in the drop-down box $PageData = Array (); The full page number collection (User drop-down box selection) $PageShowData = Array (); The set of page numbers used to display the subscript $page = Intval ($page); if ($rows = = 0) {return $data = array (' page ' = = $page, ' MaxPage ' = = $MaxPage, ' MinPage ' =& Gt $MinPage, ' serverpage ' = $ServerPage, ' pagedata ' and $PageData, ' Pageshowdata ' and $PageShowD ATA); }//Get maximum paging and min paging $MaxPage = intval ($rows/$num); $double = $rows% $num; if ($double > 0.000000001) {$MaxPage + = 1; } $MinPage = 1; $ServerPage = 1; Get all items for page number for ($i = 1; $i <= $MaxPage; $i + +) {$PageData [$i] = $i; }//correct correction of the page number if ($page < $MinPage) {$page = 1; } if ($page > $MaxPage) {$page = 1; }/* Generates the displayed page number label *//generates the code on the left of fast if ($page = = $MinPage) {$PageShowData [1] = $page; if ($page > $MinPage) {$PageShowData [1] = $MinPage;//$PageShowData [2] = $page-1;//<< $PageShowData [2] = ' << '; << if ($page-2 > $MinPage) {$PageShowData [3] = $page-2; $PageShowData [4] = $page-1; }elseif ($page-2 = = $MinPage) {$PageShowData [3] = $page-1; }//To determine the selected page number code fast $PageShowData [Count ($PageShowData) + 1] = $page; } $COUNT = COUNT ($PageShowData) + 1; Generate the code block on the right if ($page = = $MaxPage) {return $data = array (' page ' = = $page, ' MaxPage ' = $MaxPage, ' MinPage ' = $MinPage, ' serverpage ' and $ServerPage, ' pagedata ' and $PageData, ' Pageshowdata ' = $PageShowData); } if ($page < $MaxPage) {if ($MaxPage-$page >= 2) {$j = 1; for ($i = $COUNT; $i <= $length; $i + +) {$PageShowData [$i] = $page + $j; $j + +; if ($PageShowData [$i] = = $MaxPage-1) {break; }}} $COUNT = COUNT ($PageShowData) + 1;//$PageShowData [$COUNT] = $page + 1; >> $PageShowData [$COUNT] = ' >> '; $PageShowData [$COUNT + 1] = $MaxPage; Get the selected parameter $ServerPage = $PageShowData [$COUNT-1] + 1; if ($ServerPage > $MaxPage) {$ServerPage = 1; } return $data = Array (' page ' = = $page, ' MaxPage ' = = $MaxPage, ' MinPage ' = ' $ MinPage, ' serverpage ' = $ServerPage, ' pagedata ' and $PageData, ' Pageshowdata ' and $Page ShowData); } }
The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!