SoFunction
Updated on 2025-03-09

Simple paging classes and usage examples of PHP implementation

This article describes the simple paging classes and usage of PHP implementation. Share it for your reference, as follows:

<?php
/*
  * use:
  * $page = new Page (connector, query statement, current page number, size per page, page number character)
  * Connector: A MYSQL connection identifier. If the parameter is left blank, the most recent connection is used.
  * Query statement: SQL statement
  * Current page number: Specify which page is currently
  * Size per page: Number of records displayed per page
  * Page number character: Specify the current page URL format
  *
  * Example of use:
  * $sql = "select * from aa";
  * $page = new Page($conn,$sql,$_GET['page'],4,"?page=");
  *
  * Get the current page number
  * $page->page;
  *
  * Get the total number of pages
  * $page->pageCount;
  *
  * Get the total number of records
  * $page->rowCount;
  *
  * Get the number of records on this page
  * $page->listSize;
  *
  * Obtain record set
  * $page->list;
  * Recordset is a 2-dimensional array, example: list[0]['id'] accesses the id field value of the first record.
  *
  * Get a list of page numbers
  * $page->getPageList();
  */
class Page
{
  //Basic data  var $sql;
  var $page;
  var $pageSize;
  var $pageStr;
  // Statistics  var $pageCount; //Page number  var $rowCount; //Record count  //Result Data  var $list = array(); //Array of result rows  var $listSize ;
  //Constructor  function Page($conn,$sql_in,$page_in,$pageSize_in,$pageStr_in)
  {
    $this->sql = $sql_in;
    $this->page = intval($page_in);
    $this->pageSize = $pageSize_in;
    $this->pageStr = $pageStr_in;
    //If the page number is empty or less than 1, the processing    if(!$this->page||$this->page<1)
    {
      $this->page = 1;
    }
    //Query the total number of records    $rowCountSql = preg_replace("/([\w\W]*?select)([\w\W]*?)(from[\w\W]*?)/i","$1 count(0) $3",$this->sql);
    if(!$conn)
      $rs = mysql_query($rowCountSql) or die(": error on getting rowCount.");
    else
      $rs = mysql_query($rowCountSql,$conn) or die(": error on getting rowCount.");
    $rowCountRow = mysql_fetch_row($rs);
    $this->rowCount=$rowCountRow[0];
    //Calculate the total number of pages    if($this->rowCount%$this->pageSize==0)
      $this->pageCount = intval($this->rowCount/$this->pageSize);
    else
      $this->pageCount = intval($this->rowCount/$this->pageSize)+1;
    //SQL offset    $offset = ($this->page-1)*$this->pageSize;
    if(!$conn)
      $rs = mysql_query($this->sql." limit $offset,".$this->pageSize) or die(": error on listing.");
    else
      $rs = mysql_query($this->sql." limit $offset,".$this->pageSize,$conn) or die(": error on listing.");
    while($row=mysql_fetch_array($rs))
    {
      $this->list[]=$row;
    }
    $this->listSize = count($this->list);
  }
  /*
    * getPageList method generates a simpler page number list
    * If you need to customize the page number list, you can modify the code here, or use information such as total page number/total records to calculate and generate.
    */
  function getPageList()
  {
    $firstPage;
    $previousPage;
    $pageList;
    $nextPage;
    $lastPage;
    $currentPage;
    //If page number > 1, the home page connection will be displayed    if($this->page>1)
    {
      $firstPage = "&lt;a href=\"".$this-&gt;pageStr."1\">Home</a>";
    }
    //If page number > 1, the previous page connection will be displayed    if($this-&gt;page&gt;1)
    {
      $previousPage = "&lt;a href=\"".$this-&gt;pageStr.($this-&gt;page-1)."\"&gt;Previous page&lt;/a&gt;";
    }
    //If it is not reached the last page, the next page connection will be displayed    if($this-&gt;page&lt;$this-&gt;pageCount)
    {
      $nextPage = "&lt;a href=\"".$this-&gt;pageStr.($this-&gt;page+1)."\"&gt;Next page&lt;/a&gt;";
    }
    //If it is not reached the last page, the last page connection will be displayed    if($this-&gt;page&lt;$this-&gt;pageCount)
    {
      $lastPage = "&lt;a href=\"".$this-&gt;pageStr.$this-&gt;pageCount."\">Last Page</a>";
    }
    // List of all pages    for($counter=1;$counter&lt;=$this-&gt;pageCount;$counter++)
    {
      if($this-&gt;page == $counter)
      {
        $currentPage = "&lt;b&gt;".$counter."&lt;/b&gt;";
      }
      else
      {
        $currentPage = " "."&lt;a href=\"".$this-&gt;pageStr.$counter."\"&gt;".$counter."&lt;/a&gt;"." ";
      }
      $pageList .= $currentPage;
    }
    return $firstPage." ".$previousPage." ".$pageList." ".$nextPage." ".$lastPage." ";
  }
}
?&gt;

Usage example:

&lt;?php
@$db = mysql_connect('localhost', 'root', '123456') or
    die("Could not connect to database.");//Connect the databasemysql_query("set names 'utf8'");//Output Chinesemysql_select_db('test');    //Select a database$sql = "select * from `users`"; //A simple query$page = new Page('',$sql,$_GET['page'],5,"?page=");
$rows = $page-&gt;list;
foreach($rows as $row)
{
  echo $row['UserName']."&lt;br&gt;";
}
echo $page-&gt;getPageList(); //Output paging list?&gt;

For more information about PHP related content, please check out the topic of this site:Complete collection of PHP array (Array) operation techniques》、《Summary of PHP mathematical operation skills》、《Summary of usage of php regular expressions》、《PHP+ajax tips and application summary》、《Summary of PHP operations and operator usage》、《Summary of PHP network programming skills》、《Introduction to PHP basic syntax》、《Summary of the usage of php date and time》、《PHP object-oriented programming tutorial》、《Summary of usage of php strings》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php

I hope this article will be helpful to everyone's PHP programming.