Example of PHP Paging Class Based on Object Oriented Encapsulation
- 2021-11-29 23:18:00
- OfStack
In this paper, the paging class based on object-oriented encapsulation in PHP is described as an example. Share it for your reference, as follows:
<?php
class Page
{
protected $num;// Number of items displayed per page
protected $total;// Total number of records
protected $pageCount;// Total pages
protected $current;// Current page number
protected $offset;// Offset
protected $limit;// Pagination page number
/**
* Construction method
* @param int $total Total number of records
* @param int $num Number of items displayed per page
*/
public function __construct($total,$num=5)
{
//1. Number of items displayed per page
$this->num = $num;
//2. Total number of records
$this->total = $total;
//3. Total pages
$this->pageCount = ceil($total/$num);
//4. Offset
$this->offset = ($this->current-1)*$num;
//5. Pagination page number
$this->limit = "{$this->offset},{$this->num}";
//6. Initialize the current page
$this->current();
}
/**
* Initialize the current page
*/
public function current(){
$this->current = isset($_GET['page'])?$_GET['page']:'1';
// Determine the maximum range of the current page
if ($this->current>$this->pageCount){
$this->current = $this->pageCount;
}
// Determine the minimum range of the current page
if ($this->current<1){
$this->current = 1;
}
}
/**
* To access properties that you do not have permission to access
* @param string $key Attributes you want to access
* @return float|int|string Returns the corresponding condition to be changed
*/
public function __get($key){
if ($key == "limit") {
return $this->limit;
}
if ($key == "offset") {
return $this->offset;
}
if ($key == "current") {
return $this->current;
}
}
/**
* Handle paging buttons
* @return string Spliced paging buttons
*/
public function show(){
// Determine the initial page number
$_GET['page'] = isset($_GET['page'])?$_GET['page']:'1';
// Will $_GET Values are assigned to upper and lower variables
$first = $end = $prev = $next = $_GET;
// var_dump($prev);
// Upper 1 Page
// On judgment 1 Page range
if ($this->current-1<1){
$prev['page'] = 1;
}else{
$prev['page'] = $this->current-1;
}
// Under 1 Page
// Under judgment 1 Page range
if ($this->current+1>$this->pageCount) {
$next["page"] = $this->pageCount;
}else{
$next['page'] = $this->current+1;
}
/*
Home page
$first['page'] = 1;
// End page
$end['page'] = $this->pageCount;
*/
// Splice path
$url = "http://".$_SERVER["SERVER_NAME"].$_SERVER["SCRIPT_NAME"];
// Splice array url Address bar suffix ? Pass in parameters
//http://xxx/xxx/Page.class.php?page= Value
$prev = http_build_query($prev);
$next = http_build_query($next);
// $first = http_build_query($first);
// $end = http_build_query($end);
// Splice full path
$prevpath = $url."?".$prev;
$nextpath = $url."?".$next;
// $firstpath = $url."?".$first;
// $endpath = $url."?".$end;
$str = " Shared {$this->total} A record Shared {$this->pageCount} Page ";
$str .= "<a href='{$url}?page=1'> Home page </a> ";
$str .= "<a href='{$prevpath}'> Upper 1 Page </a> ";
$str .= "<a href='{$nextpath}'> Under 1 Page </a> ";
$str .= "<a href='{$url}?page={$this->pageCount}'> End page </a> ";
return $str;
}
}
// Self-debugging
$a = new Page(10);
echo $a->show();
?>
More readers interested in PHP can check the topics of this site: "php+mysql Database Operation Introduction Tutorial", "php+mysqli Database Programming Skills Summary", "php Object-Oriented Programming Introduction Tutorial", "PHP Array (Array) Operation Skills Encyclopedia", "php String (string) Usage Summary", "PHP Network Programming Skills Summary" and "php Common Database Operation Skills Summary"
I hope this article is helpful to everyone's PHP programming.