当前位置: 移动技术网 > IT编程>开发语言>PHP > PHP实现可自定义样式的分页类

PHP实现可自定义样式的分页类

2017年12月12日  | 移动技术网IT编程  | 我要评论

我爱河东狮下载,木制装饰品,网曝江西斗狗团体

本文实例为大家分享了php实现可自定义样式的分页类,供大家参考,具体内容如下

<?php
 
//namespace component;
/**
 * 2016-3-27
 * @author ankang
 */
class page {
 private $showpage;
 private $countpage;
 private $floorp;
 private $pageurl;
 private $pageclass;
 private $curclass;
 
 /**
 * @author ankang
 * @param number $countnum  数据总数
 * @param string $pageurl  跳转链接
 * @param string $pageclass  <a>标签 总体样式 
 * @param string $pageurl  当前页样式
 * @param number $pagesize  每页显示的数据条数
 * @param number $showpage  每次显示的页数 
 */
 public function __construct($countnum, $pageurl = null, $pageclass = null,$curclass = null, $pagesize = 20, $showpage = 5) {
 $this->showpage = $showpage;
 $this->countpage  = ceil ( $countnum / $pagesize );
 $this->floorp  = floor ( $showpage / 2 ); // 偏移量 
 $this->pageclass  = is_null ( $pageclass ) ? '' : $pageclass;
 $this->curclass = is_null ( $curclass ) ? '' : $curclass;
  
 // $serverurl  = ( preg_match('/\?/i', $_server['request_uri']))?preg_replace('/\&p\=[0-9]+/i', "", $_server['request_uri']) : $_server['request_uri']."?";
 // if( substr($buturl,0,2)=='//' ){
  // $serverurl  = substr($serverurl,1);
 // }
 // $url   = preg_replace('/p=[\d]*/i', '', $serverurl);
  $url   = '';
 //推荐自己传url,不传也可以打开上面的代码自动获取
 $this->pageurl  = is_null ( $pageurl ) ? $url : $pageurl;
 }
 
 /**
 *
 * @param number $page  
 * @param string $showtopage
 *  首页,上下页,尾页
 * @param string $html 标签元素,li,p 
 * @return string
 */
 public function getpage($page = 1, $showtopage = true, $html = null) {
 $startpage  = ($page - $this->floorp); // 开始页码
 $endpage  = ($page + $this->floorp); // 结束页码
  
 if ($this->countpage < $this->showpage) {
  $startpage = 1;
  $endpage = $this->countpage;
 }
  
 if ($startpage < 1) {
  $startpage = 1;
  $endpage = $this->showpage;
 }
  
 if ($endpage > $this->countpage) {
  $startpage = $this->countpage - $this->showpage + 1;
  $endpage = $this->countpage;
 }
  
 $pagehtml = '';
  
 if (! is_null ( $html )) {
  if ($html == 'li') {
  $shtml = '<li>';
  $ehtml = '</li>';
  } else {
  $shtml = '<p>';
  $ehtml = '</p>';
  }
 }
  
 if (true == $showtopage) {
  $pagehtml  .= "$shtml<a href='{$this->pageurl}p=1'>« 首页</a>$ehtml";
  $prveurl   = $this->getprve($page);
  $pagehtml  .= "$shtml<a href='{$prveurl}'>« 上一页</a>$ehtml";
 }
  
 for($i = $startpage; $i <= $endpage; $i ++) {
  if ($page == $i) {
  $pagehtml  .= "$shtml<a href='{$this->pageurl}p={$i}' class='{$this->curclass}'>{$i}</a>$ehtml";
  } else {
  $pagehtml  .= "$shtml<a href='{$this->pageurl}p={$i}' class='{$this->pageclass}'>{$i}</a>$ehtml";
  }
 }
  
 if (true == $showtopage) {
  $nexturl   = $this->getnext($page);
  $pagehtml  .= "$shtml<a href='{$nexturl}'>下一页 »</a>$ehtml";
  $pagehtml  .= "$shtml<a href='{$this->pageurl}p={$this->countpage}' >尾页 »</a>$ehtml";
 }
  
 return $pagehtml;
 }
 
 public function getprve($page){
 if ($page != 1) {
  $prve  = $page - 1;
  $prveurl  = "{$this->pageurl}p={$prve}";
 } else {
  $prveurl  = "{$this->pageurl}p=1";
 }
  
 return $prveurl;
 }
 
 public function getnext($page){
 if ($page != $this->countpage) {
  $next  = $page + 1;
  $nexturl  = "{$this->pageurl}p={$next}";
 } else {
  $nexturl  = "{$this->pageurl}p={$this->countpage}";
 }
  
 return $nexturl;
 }
 
 
 
}

再为大家分享一个主要用于新手学习php分页,代码简单实用,主要是注释很完整。

1. page.class.php

<?php
/**
 * 分页类
 * 
 * 调用方式:
 * $p=new page(总页数,显示页数,当前页码,每页显示条数,[链接]);
 * print_r($p->getpages()); //生成一个页码数组(键为页码,值为链接)
 * echo $p->showpages(1); //生成一个页码样式(可添加自定义样式)
 * 
 * @author: dzer <email:358654744@qq.com blog:dzer.me>
 * @version: 2014-12-25 09:09:42
 * @last modified time: 2014-12-28 17:37:13
 */
 
/*
思路:
给我一个 总页数,需要显示的页数,当前页,每页显示的条数,连接
写一个方法 生成一个一维数组,键为页码 值为连接
写一个方法 返回一个生成好样式的页码(并且可以根据自己需要添加样式)
默认样式 共45条记录,每页显示10条,当前第1/4页 [首页] [上页] [1] [2] [3] .. [下页] [尾页]
*/
class page{
 protected $count;  //总条数
 protected $showpages; //需要显示的页数
 protected $countpages; //总页数
 protected $currpage; //当前页
 protected $subpages; //每页显示条数
 protected $href;  //连接
 protected $page_arr=array(); //保存生成的页码 键页码 值为连接
 
 /**
  * __construct 构造函数(获取分页所需参数)
  * @param int $count  总条数
  * @param int $showpages 显示页数
  * @param int $currpage 当前页数
  * @param int $subpages 每页显示数量
  * @param string $href 连接(不设置则获取当前url)
  */
 public function __construct($count,$showpages,$currpage,$subpages,$href=''){
  $this->count=$count;
  $this->showpages=$showpages;
  $this->currpage=$currpage;
  $this->subpages=$subpages;
   
  //如果链接没有设置则获取当前连接
  if(empty($href)){
   $this->href=htmlentities($_server['php_self']); 
  }else{
   $this->href=$href;
  }
  $this->construct_pages();
 }
 
 /**
  * getpages 返回页码数组
  * @return array 一维数组 键为页码 值为链接
  */
 public function getpages(){
  return $this->page_arr;
 }
 
 /**
  * showpages 返回生成好的页码
  * @param int $style 样式
  * @return string  生成好的页码
  */
 public function showpages($style=1){
  $func='pagestyle'.$style;
  return $this->$func();
 }
 
 /**
  * pagestyle1 分页样式(可参照这个添加自定义样式 例如pagestyle2())
  * 样式 共45条记录,每页显示10条,当前第1/4页 [首页] [上页] [1] [2] [3] .. [下页] [尾页] 
  * @return string 
  */
 protected function pagestyle1(){
  /* 构造普通模式的分页 
  共4523条记录,每页显示10条,当前第1/453页 [首页] [上页] [1] [2] [3] .. [下页] [尾页] 
  */
  $pagestr='共'.$this->count.'条记录,每页显示'.$this->subpages.'条';
  $pagestr.='当前第'.$this->currpage.'/'.$this->countpages.'页 ';
 
  $_get['page'] = 1;
  $pagestr.='<span>[<a href="'.$this->href.'?'.http_build_query($_get).'">首页</a>] </span>';
  //如果当前页不是第一页就显示上页
  if($this->currpage>1){
   $_get['page'] = $this->currpage-1;
   $pagestr.='<span>[<a href="'.$this->href.'?'.http_build_query($_get).'">上页</a>] </span>';
  }
 
  foreach ($this->page_arr as $k => $v) {
   $_get['page'] = $k;
   $pagestr.='<span>[<a href="'.$v.'">'.$k.'</a>] </span>';
  }
 
  //如果当前页小于总页数就显示下一页
  if($this->currpage<$this->countpages){
   $_get['page'] = $this->currpage+1;
   $pagestr.='<span>[<a href="'.$this->href.'?'.http_build_query($_get).'">下页</a>] </span>';
  }
 
  $_get['page'] = $this->countpages;
  $pagestr.='<span>[<a href="'.$this->href.'?'.http_build_query($_get).'">尾页</a>] </span>';
 
  return $pagestr;
 }
 
 /**
  * construct_pages 生成页码数组
  * 键为页码,值为链接
  * $this->page_arr=array(
  *     [1] => index.php?page=1
  *     [2] => index.php?page=2
  *     [3] => index.php?page=3
  *     ......)
  */
 protected function construct_pages(){
  //计算总页数
  $this->countpages=ceil($this->count/$this->subpages);
  //根据当前页计算前后页数
  $leftpage_num=floor($this->showpages/2);
  $rightpage_num=$this->showpages-$leftpage_num;
 
  //左边显示数为当前页减左边该显示的数 例如总显示7页 当前页是5 左边最小为5-3 右边为5+3
  $left=$this->currpage-$leftpage_num;
  $left=max($left,1); //左边最小不能小于1
  $right=$left+$this->showpages-1; //左边加显示页数减1就是右边显示数
  $right=min($right,$this->countpages); //右边最大不能大于总页数
  $left=max($right-$this->showpages+1,1); //确定右边再计算左边,必须二次计算
   
  for ($i=$left; $i <= $right; $i++) {
   $_get['page'] = $i;
   $this->page_arr[$i]=$this->href.'?'.http_build_query($_get);
  }
 }
}

2. demo.php 

<?php
/**
 * 分页类demo
 * be the best of whatever you are!
 * 
 * @author: dzer<358654744@qq.com>
 * @version: 2014-12-28 17:38:23
 * @last modified time: 2014-12-28 18:08:28
 */
header("content-type:text/html;charset=utf8");
include('./page.class.php'); //引入类
 
//$p=new page(总页数,显示页数,当前页码,每页显示条数,[链接]);
//连接不设置则为当前链接
$page=isset($_get['page']) ? $_get['page'] : 1;
$p=new page(100,7,$page,8);
 
//生成一个页码数组(键为页码,值为链接)
echo "<pre>";
print_r($p->getpages()); 
 
//生成一个页码样式(可添加自定义样式)
//样式 共45条记录,每页显示10条,当前第1/4页 [首页] [上页] [1] [2] [3] .. [下页] [尾页]
echo $p->showpages(1); 

以上就是本文的全部内容,希望对大家学习php程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网