PHP paging class example PHP and mysql simple paging class code
Release: 2016-07-25 08:51:30
Original
914 people have browsed it
-
- 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
|
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31