-
-
/**
- * Simple paging class
- * Page.class.php
- */
- class Page
- {
- private $page_num; //Number of messages displayed on each page
- private $page_all_no; //Total number of messages
- private $page_len; //How many page numbers are displayed
- private $page; //Current page number
- private $page_max; //Maximum number of pages
- private $page_no_array; //Array of page numbers
- public $start_num; // The starting value of the query statement limit
- private $page_change; //At which page number the page number starts to increase
- private $URL; //Get the URL of the current page
public function __construct($page_all_no, $page_num=5, $page_len=5)
- {
- $this->page_all_no = intval($page_all_no);
- $this->page_num = intval($page_num);
- $this->page_len = intval( $page_len);
- $this->URL = $_SERVER['REQUEST_URI'];
- $this->max_page(); //Get the maximum number of pages
- $this->page_no(); // Get the current page number
- $this->page_no_array(); //Page number array
- $this->start_num(); //Get the starting value of limit in the sql statement
- $this->change_page(); //Get the page number where the increment starts $this->page_change
- $this->getURL(); //Get the current URL and process it and return
- }
private function isArray($str ,$str_self)
- {
- if(!is_array($str)) throw new Exception("$str_self must be an Array type");
- }
private function page_no()
- {
- $this->page = isset($_GET['page']) ? $_GET['page'] : 1;
- if(isset($_GET['page']) && $_GET['page'] page = 1;
- if(isset($_GET['page']) && $_GET['page'] > $this->page_max) $this->page = $this->page_max;
- return $this->page;
- }
private function max_page()
- {
- return $this->page_max = $this->page_all_no page_all_no/$this->page_num);
- }
private function change_page()
- {
- return $this-> page_change = ceil($this->page_len / 2);
- }
private function page_no_array()
- {
- return $this->page_no_array = range(1, $this-> ;page_max);
- }
private function start_num()
- {
- return $this->start_num = $this->page_num * ($this->page - 1);
- }
private function getURL()
- {
- if(!empty($_SERVER['argc']) ? $_SERVER['argc'] == 0 : strpos($_SERVER[ 'REQUEST_URI'], '?') === false)
- {
- $this->URL = $this->URL.'?';
- }else{
- $url_a = "/?page=[0 -9]{1,}/";
- $url_b = "/&page=[0-9]{1,}/";
- ereg("?page=[0-9]{1,}", $this ->URL) ? $this->URL = preg_replace($url_a, "?", $this->URL):
- ereg("&page=[0-9]{1,}", $this- >URL) ? $this->URL = preg_replace($url_b, "&", $this->URL):$this->URL = $this->URL.'&';
- }
- return $this->URL;
- }
private function header_info($total_data_modifier='Total number:', $total_page_modifier='Total number of pages:', $current_page_modifier='Current page:' , $header_info_modifier='', $header_data_color = 'red'){
- if($this->page_max != 1 && $this->page_all_no != 0)
- {
- $header_info = $total_data_modifier.''.$this->page_all_no.'';
- $header_info .= $total_page_modifier.''.$this->page_max.'';
- $header_info .= $current_page_modifier.''.$this->page.' ."rn";
- if(!empty($ header_info_modifier)) $header_info = $header_info_modifier.$header_info;
- return $header_info;
- }else{
- return NULL;
- }
- }
private function first($first_format = 'First page ')
- {
- return $this->page == 1 ? $first_format."rn" : ''.$first_format.''."rn";
- }
private function last($last_format = '上一页')
- {
- if($this->page == 1) return $last_format."rn";
- return $this->page-1 == 1 ? ''.$last_format.''."rn" : ''.$last_format.''."rn";
- }
private function page_num_format($separator=' ', $left_modifier='[', $right_modifier=']', $both_sides=false, $current_page_color = 'red')
- {
- empty($separator) ? $separator = ' ' : $separator;
- $page_array = '';
- if($this->page_max page_len)
- {
- for ($i=0; $i page_max; $i++)
- {
- if($this->page_no_array[$i] == $this->page)
- {
- $the[$i] = ''.$left_modifier.$this->page_no_array[$i].$right_modifier.''."rn";
- }else{
- if($i == 0)
- {
- $page_one = substr($this->URL,0,strlen($this->URL)-1);
- $the[$i] = ''.$left_modifier.$this->page_no_array[$i].$right_modifier.''."rn";
- }else{
- $the[$i] = ''.$left_modifier.$this->page_no_array[$i].$right_modifier.''."rn";
- }
- }
- $page_array .= $the[$i].$separator;
- }
- if($both_sides === false)
- {
- $page_array = substr($page_array,0,strrpos($page_array, $separator));
- }elseif ($both_sides === true){
- $page_array = $separator.$page_array;
- }else{
- throw new Exception('ERROR: $both_sides must be a boolean type.');
- }
- }else{
- if($this->page page_change)
- {
- $i_start = 0;
- }else{
- $i_start = $this->page - $this->page_change;
- //如果最大的页码已显示,那么开始页就不会在递增
- if($i_start >= $this->page_max - $this->page_len){
- $i_start = $this->page_max - $this->page_len;
- }
- }
- $i_end = ($i_start+$this->page_len) - 1;
- for ($i = $i_start; $i {
- if($this->page_no_array[$i] == $this->page)
- {
- $the[$i] = ''.$left_modifier.$this->page_no_array[$i].$right_modifier.''."rn";
- }else{
- if($i == 0)
- {
- $page_one = substr($this->URL,0,strlen($this->URL)-1);
- $the[$i] = ''.$left_modifier.$this->page_no_array[$i].$right_modifier.''."rn";
- }else{
- $the[$i] = ''.$left_modifier.$this->page_no_array[$i].$right_modifier.''."rn";
- }
- }
- $page_array .= $the[$i].$separator;
- }
- if($both_sides === false)
- {
- $page_array = substr($page_array,0,strrpos($page_array, $separator));
- }elseif ($both_sides === true){
- $page_array = $separator.$page_array;
- }else{
- throw new Exception('ERROR: $both_sides must be a boolean type.');
- }
- }
- return $page_array;
- }
private function next($next_format = '下一页')
- {
- if($this->page >= $this->page_max) return $next_format."rn";
- return ''.$next_format.''."rn";
- }
private function end($end_format = '最后一页')
- {
- return $this->page >= $this->page_max ? $end_format."rn" : ''.$end_format.''."rn";
- }
public function select_page($target='self', $select_page_mode='PMA', $value_left_modifier='', $value_right_modifier='')
- {
- if($this->page_max != 1 && $this->page_all_no != 0)
- {
- if($select_page_mode === 'NORMAL')
- {
- $page_no_array = $this->page_no_array;
- }elseif($select_page_mode === 'PMA'){
- $page_no_array = $this->PMA_page_no_array();
- }else{
- throw new Exception('$select_page_mode is unknown mode');
- }
- $select_page = '';
- $select_page .= ''. "n";
- return $select_page;
- }else{
- return NULL;
- }
- }
private function PMA_page_no_array()
- {
- $showAll = 200;
- $sliceStart = 5;
- $sliceEnd = 5;
- $percent = 20;
- $range = 10;
if ($this->page_max $this->PMA_page_no_array = range(1, $this->page_max);
- } else {
- $this->PMA_page_no_array = array();
- for ($i = 1; $i $this->PMA_page_no_array[] = $i;
- }
- for ($i = $this->page_max - $sliceEnd; $i page_max; $i++) {
- $this->PMA_page_no_array[] = $i;
- }
- $i = $sliceStart;
- $x = $this->page_max - $sliceEnd;
- $met_boundary = false;
- while ($i if ($i >= ($this->page - $range) && $i page + $range)) {
- $i++;
- $met_boundary = true;
- } else {
- $i = $i + floor($this->page_max / $percent);
- if ($i > ($this->page - $range) && !$met_boundary) {
- $i = $this->page - $range;
- }
- }
- if ($i > 0 && $i $this->PMA_page_no_array[] = $i;
- }
- }
- sort($this->PMA_page_no_array);
- $this->PMA_page_no_array = array_unique($this->PMA_page_no_array);
- }
- return $this->PMA_page_no_array;
- }
public function key_change_page(){
- echo '';
- }
public function eshow()
- {
- return $this->last().$this->next();
- }
public function showForHelp()
- {
- return $this->first('首页', '').$this->last('上一页', '').$this->page_num_format('', '', '', false, '#FF8500').$this->next('下一页', '').$this->end('尾页', '');
- }
public function show()
- {
- return $this->header_info().$this->first('首页', '').$this->last('上一页', '').$this->page_num_format('', '', '', false, '#FF8500').$this->next('下一页', '').$this->end('尾页', '');
- }
- }
-
复制代码
2. How to use php paging class:
-
- header('content-type:text/html;charset=utf-8');
- include('Page.class.php');
- $page_all_no = 12000; //Total number of data items
- $page_num = 25; //Set the number of items displayed on each page
- $page_len = 7; //The maximum number of page numbers to display
- $page = new Page($page_all_no, $page_num, $page_len);
- $start_num = $ page->start_num;
- $sql = "select * from table limit {$start_num}, {$page_num}";
- $page->key_change_page(); //arrow keys to turn pages
- var_dump($sql);
- //It comes with three paging forms, you can add new methods as needed, call
- echo '
'.$page->select_page('self', 'NORMAL').''; //The second parameter here defaults to PMA mode. The difference between the two modes is given above
- echo '
'.$page->eshow().' > ;';
- echo '
'.$page->show().' ';
Copy code
3, PHP paging class, rendering:
|