-
- class PageModel {
- /**
- * Get the paging array
- * @param unknown $page The current number of pages
- * @param unknown $goodsCount The total number of products
- * @param unknown $pageLength The number of pages displayed on each page
- */
- public static function getPageArr($page, $goodsCount, $pageCountLength, $pageLength) {
- //Total number of pages
- $allPageCount = ceil($goodsCount / $pageLength);
- //If the page is always shorter than the length, set the page length to the total number of pages
- if ($allPageCount <= $pageCountLength) {
- $allPageCount = ceil($goodsCount / $pageLength);
- }
- //The total number of pages is displayed in one page
- if ($allPageCount <= $pageCountLength) {
- for ($i = 0; $i < $allPageCount; $i ++) {
- $arr[] = array( 'page' => $i + 1);
- }
- return $arr;
- }
- //The length before and after
- $halfLength = floor($pageCountLength / 2);
- //Because it is too small, put it in the original position , left
- if ($page <= $halfLength) {
- $arr = array();
- for ($i = 0; $i < $pageCountLength; $i ++) {
- $arr[] = array ('page' => $i + 1);
- }
- return $arr;
- }
- //If it is too big, only the edge will be fetched. If it exceeds, only the edge will be fetched
- if ($page > $allPageCount - floor ($pageCountLength / 2)) {
- for ($i = -$pageCountLength; $i < 0; $i ++) {
- $arr[] = array('page' => $allPageCount + $i + 1);
- }
- return $arr;
- }
- //The middle number, take out the middle number
- for ($i = -$halfLength; $i < $pageCountLength - $halfLength; $i ++) {
- $arr[] = array('page' => $page + $i);
- }
- return $arr;
- }
- }
Copy code
2. PHP paging class code
Code:
-
-
class Helper_Page{
/**Total number of messages*/
- var $infoCount;
- /**total pages*/
- var $pageCount ;
- /**Number of items displayed per page*/
- var $items;
- /**Current page number*/
- var $pageNo;
- /**The starting position of the query*/
- var $startPos;
- /**Next page */
- var $nextPageNo;
- /**Previous page*/
- var $prevPageNo;
function Helper_Page($infoCount, $items, $pageNo)
- {
- $this-> ;infoCount = $infoCount;
- $this->items = $items;
- $this->pageNo = $pageNo;
- $this->pageCount = $this->GetPageCount();
- $this-> ;AdjustPageNo();
- $this->startPos = $this->GetStartPos();
- }
- function AdjustPageNo()
- {
- if($this->pageNo == '' || $this-> ;pageNo < 1)
- $this->pageNo = 1;
- if ($this->pageNo > $this->pageCount)
- $this->pageNo = $this->pageCount;
- }
- /**
- * Next page
- */
- function GoToNextPage()
- {
- $nextPageNo = $this->pageNo + 1;
- if ($nextPageNo > $this->pageCount)
- {
- $this- >nextPageNo = $this->pageCount;
- return false;
- }
- $this->nextPageNo = $nextPageNo;
- return true;
- }
- /**
- * Previous page
- */
- function GotoPrevPage()
- {
- $prevPageNo = $this->pageNo - 1;
- if ($prevPageNo < 1)
- {
- $this->prevPageNo = 1;
- return false;
- }
- $this->prevPageNo = $prevPageNo ;
- return true;
- }
- function GetPageCount()
- {
- return ceil($this->infoCount / $this->items);
- }
- function GetStartPos()
- {
- return ($this-> pageNo - 1) * $this->items;
- }
- }
-
Copy code
|