一个php分页类代码(附效果图)

发布时间:2020-09-18编辑:脚本学堂
分享一个php分页类代码,一个完整的php分页类,带有演示实例,附有分页效果图,感兴趣的朋友参考下。

php分页代码实例。

1,分页类Page.class.php
 

复制代码 代码示例:

/**
* 简单分页类
* Page.class.php
*/
class Page
{
private $page_num; //每页显示的信息条数
private $page_all_no; //信息的总条数
private $page_len; //显示多少个页码
private $page; //当前的页数
private $page_max; //页数最大值
private $page_no_array; //页数的数组
public $start_num; //查询语句limit的起始值
private $page_change; //在第几个页码开始 页码递增
private $URL; //获取当前页面的URL

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(); //得到页数的最大值
$this->page_no(); //得到当前页数
$this->page_no_array(); //页数数组
$this->start_num(); //得到sql语句中limit的起始值
$this->change_page(); //得到递增开始的页码$this->page_change
$this->getURL(); //得到当前的URL并处理返回
}

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'] < 1) $this->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 <= 0 ? 1 : ceil($this->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_page_modifier=' 总页数:', $current_page_modifier=' 当前页:', $header_info_modifier='', $header_data_color = 'red'){
if($this->page_max != 1 && $this->page_all_no != 0)
{
$header_info = $total_data_modifier.'<font color="'.$header_data_color.'"><b>'.$this->page_all_no.'</b></font>';
$header_info .= $total_page_modifier.'<font color="'.$header_data_color.'"><b>'.$this->page_max.'</b></font>';
$header_info .= $current_page_modifier.'<font color="'.$header_data_color.'"><b>'.$this->page.'</b></font> '."rn";
if(!empty($header_info_modifier)) $header_info = $header_info_modifier.$header_info;
return $header_info;
}else{
return NULL;
}
}

private function first($first_format = '第一页')
{
return $this->page == 1 ? $first_format."rn" : '<a id="kl_first" href="'.substr($this->URL,0,strlen($this->URL)-1).'">'.$first_format.'</a>'."rn";
}

private function last($last_format = '上一页')
{
if($this->page == 1) return $last_format."rn";
return $this->page-1 == 1 ? '<a id="kl_last" href="'.substr($this->URL,0,strlen($this->URL)-1).'">'.$last_format.'</a>'."rn" : '<a id="kl_last" href="'.$this->URL.'page='.($this->page - 1).'">'.$last_format.'</a>'."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 <= $this->page_len)
{
for ($i=0; $i < $this->page_max; $i++)
{
if($this->page_no_array[$i] == $this->page)
{
$the[$i] = '<font id="kl_cur_page" href="javascript:void(0);" color="'.$current_page_color.'"><b>'.$left_modifier.$this->page_no_array[$i].$right_modifier.'</b></font>'."rn";
}else{
if($i == 0)
{
$page_one = substr($this->URL,0,strlen($this->URL)-1);
$the[$i] = '<a href="'.$page_one.'">'.$left_modifier.$this->page_no_array[$i].$right_modifier.'</a>'."rn";
}else{
$the[$i] = '<a href="'.$this->URL.'page='.$this->page_no_array[$i].'">'.$left_modifier.$this->page_no_array[$i].$right_modifier.'</a>'."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 <= $this->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 <= $i_end; $i++)
{
if($this->page_no_array[$i] == $this->page)
{
$the[$i] = '<font id="kl_cur_page" color="'.$current_page_color.'"><b>'.$left_modifier.$this->page_no_array[$i].$right_modifier.'</b></font>'."rn";
}else{
if($i == 0)
{
$page_one = substr($this->URL,0,strlen($this->URL)-1);
$the[$i] = '<a href="'.$page_one.'">'.$left_modifier.$this->page_no_array[$i].$right_modifier.'</a>'."rn";
}else{
$the[$i] = '<a href="'.$this->URL.'page='.$this->page_no_array[$i].'">'.$left_modifier.$this->page_no_array[$i].$right_modifier.'</a>'."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 '<a id="kl_next" href="'.$this->URL.'page='.($this->page + 1).'">'.$next_format.'</a>'."rn";
}

private function end($end_format = '最后一页')
{
return $this->page >= $this->page_max ? $end_format."rn" : '<a id="kl_end" href="'.$this->URL.'page='.$this->page_max.'">'.$end_format.'</a>'."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 = '<script type="text/javascript">
function Page_jumpMenu(targ,selObj,restore){
    eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
    if (restore) selObj.selectedIndex=0;
}
</script>';
$select_page .= '<select name="Page_jumpMenu" id="Page_jumpMenu" onchange="Page_jumpMenu(''.$target.'',this,0)">'. "n";
foreach($page_no_array as $value)
{
if($value == 1)
{
$false_url = substr($this->URL,0,strlen($this->URL)-1);
$select_page .= '<option value="'.$false_url.'">'.$value_left_modifier.$value.$value_right_modifier.'</option>'. "n";
}else{
if($value == $this->page){
$select_page .= '<option value="'.$this->URL.'page='.$value.'"  selected="selected">'.$value_left_modifier.$value.$value_right_modifier.'</option>'. "n";
}else{
$select_page .= '<option value="'.$this->URL.'page='.$value.'">'.$value_left_modifier.$value.$value_right_modifier.'</option>'. "n";
}
}
}
$select_page .= '</select>'. "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 < $showAll){
$this->PMA_page_no_array = range(1, $this->page_max);
} else {
$this->PMA_page_no_array = array();
for ($i = 1; $i <= $sliceStart; $i++) {
$this->PMA_page_no_array[] = $i;
}
for ($i = $this->page_max - $sliceEnd; $i <= $this->page_max; $i++) {
$this->PMA_page_no_array[] = $i;
}
$i = $sliceStart;
$x = $this->page_max - $sliceEnd;
$met_boundary = false;
while ($i <= $x) {
if ($i >= ($this->page - $range) && $i <= ($this->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 <= $x) {
$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 '<script type="text/javascript">
 document.onkeydown=Page_keypress;
 function Page_keypress(e){
     if(!e)var e = window.event;
     var kc = e.which||e.keyCode;
     switch(kc){
 case 37:
 if(document.getElementById("kl_last") != null) location.href=document.getElementById("kl_last").href;
 break;
 case 39:
 if(document.getElementById("kl_next") != null) location.href=document.getElementById("kl_next").href;
 break;
     }
 }
 </script>';
}

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,php分页类的使用方法:
 

复制代码 代码示例:
header('content-type:text/html;charset=utf-8');
include('Page.class.php');
$page_all_no = 12000; //数据的总条数
$page_num = 25; //设置每页显示的条数
$page_len = 7; //最多显示的页码数
$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(); //方向键翻页
var_dump($sql);
//自带三种分页形式,可再按需要添加新方法,像下面那样调用
echo '<p>'.$page->select_page('self', 'NORMAL').'</p>'; //这里的第二个参数默认为PMA模式,两种模式差别在上面已给出
echo '<p>'.$page->eshow().'</p>';
echo '<p>'.$page->show().'</p>';

3,php分页类,效果图:
php分页类代码