php分页实例程序函数
在php中分页是我们开发中必须要用到的东西,但在觉得在php分页比在asp中方便了很多,下面我来给各位同学详细介绍人几个不错的php分页类吧,这是我自己写的一个php分页实例,代码如下:
- <html>
- <head>
- <title>简单的PHP分页程序</title>
- </head>
- <body>
- <?php
- //建立连接数据库
- $linkphpfensi.com","root","") or die("you could notconnect mysql");
- //连接的数据库名称
- @mysql_select_db("ceshi") or die("could not select database!");
- //取得记录总数
- $query="SELECT count(*) FROM user";
- $rs = mysql_query($query);
- $myrows=mysql_fetch_array($rs);
- $numrows=$myrows[0];
- //设定每一页显示的记录数
- $pagesize = 1;
- //计算总页数
- $pages=intval($numrows/$pagesize);
- if($numrows%$pagesize)
- $pages++;
- //设置页数
- if(isset($_POST['page']))
- $page=intval($_POST['page']);
- else{
- $page = 1; //没有页数则显示第一页;
- }
- //计算记录偏移量
- $offset = ($page-1)*$pagesize;
- //读取指定的记录数
- $query1="select * from user limit $offset,$pagesize";
- $rs = mysql_query($query1);
- if($myrows=mysql_fetch_array($rs))
- {
- $i = 0;
- ?>
- <table width="80%" >
- <tr>
- <td width="50%">用户名</td>
- <td width="50%">密码</td>
- </tr>
- <?php
- do{
- $i++;
- ?>
- <tr>
- <td width="50%"><?php echo $myrows["username"] ?></td>
- <td width="50%"><?php echo $myrows["password"] ?></td>
- </tr>
- <?php
- }
- while($myrows=mysql_fetch_array($rs));
- echo "</table>";
- }
- $first = 1;
- $prev =$page-1;
- $next = $page+1;
- $last=$pages;
- if($page>1)
- {
- echo "<a href='fenye.php?page=".$first."'>首页</a>";
- echo "<a href='fenye.php?page=".$prev."'>上一页</a>";
- }
- if($page<$pages)
- {
- echo "<a href='fenye.php?page=".$next."'>下一页</a>";
- echo "<a href='fenye.php?page=".$last."'>最后一页</a>";
- }
- echo "<div align = 'center'>共有" .$pages. "页(" .$page. "/" .$pages.")";
- for($i=1; $i<$page; $i++)
- echo "<a href='fenye.php?page=".$i."'>[".$i."]</a>";
- echo "[" .$page. "]";
- for($i=$page+1; $i<=$pages;$i++)
- echo "<a href='fenye.php?page=".$i."'>[".$i."]</a>";
- echo"</div>";
- ?>
- </body>
- </html>
上面代码不能重复使用,后来整理了一个类,CSS代码如下:
- #pages{display:block; text-align:center; overflow:hidden; color:#000; font-size:13px}
- #pages a{color:#333; text-decoration:none; font-family:Verdana,Geneva,sans-serif; padding:2px 3px; display:block}
- #pages li{float:left; display:inline-block; border:1px solid #999; margin-right:3px; margin-left:3px}
- #pages #spages{background:#CCC; font-weight:bold; color:#36C}
PHP分页函数类源码如下:
- <?php
- /*
- * 分页模块
- */
- interface Page{
- public function showpage();
- }
- class AdminPage implements Page{
- /*
- * 构造参数
- * SQL语句,每页显示数,url
- */
- private $sql;
- private $pageline; //每页显示多少行数据
- private $urlstr;
- private $nowpage;
- private $totalNum;
- private $pageNum;
- private $pageStr;// 形成分页字符串
- public function __construct($sql,$p){
- $this->sql = $sql;
- $this->pageline = $p;
- $this->urlstr = $_SERVER['SCRIPT_NAME'];
- if(emptyempty($_GET['p'])){
- $this->nowpage = 1; //初始化当前页数为1
- }else if(is_numeric($_GET['p'])){
- $this->nowpage = $_GET['p'];
- }else{
- $e = new Error(6);
- $e->show();
- }
- $s = new Sql();
- $this->totalNum = $s->results_exist_num($this->sql);
- $this->pageNum = ceil($this->totalNum/$this->pageline);
- }
- /*
- * 形式为:
- * 首页| 上一页| 1 | 2 | 3 | 4 | 5 |下一页 | 末页
- *
- *
- */
- public function showpage(){
- if($this->pageNum >=1 && $this->nowpage <5 ){
- if( $this->pageNum <=5){
- $this->pageStr = '<ul ><li><a href="'.$this->urlstr.'?p=1" target="rframe" >首页</a></li>';
- if($this->nowpage!=1){
- $this->pageStr .= '<li><a href="'.$this->urlstr.'?p='.($this->nowpage-1).'" target="rframe" >上一页</a></li>';
- }
- for($i=1;$i<=$this->pageNum;$i++){
- if($i==$this->nowpage){
- $this->pageStr .='<li ><a href="'.$this->urlstr.'?p='.$i.'" target="rframe" >'.$i.'</a></li>';
- }else{
- $this->pageStr .='<li><a href="'.$this->urlstr.'?p='.$i.'" target="rframe" >'.$i.'</a></li>';
- }
- }
- if($this->nowpage !=$this->pageNum){
- $this->pageStr .= '<li><a href="'.$this->urlstr.'?p='.($this->nowpage+1).'" target="rframe" >下一页</a></li>';
- }
- $this->pageStr .='<li><a href="'.$this->urlstr.'?p='.$this->pageNum.'" target="rframe" >末页</a></li>';
- echo $this->pageStr;
- }else if($this->pageNum>5){
- $this->pageStr = '<ul ><li><a href="'.$this->urlstr.'?p=1" target="rframe" >首页</a></li>';
- if($this->nowpage !=1){
- $this->pageStr .= '<li><a href="'.$this->urlstr.'?p='.($this->nowpage-1).'" target="rframe" >上一页</a></li>';
- }
- for($i=1;$i<=5;$i++){
- if($i==$this->nowpage){
- $this->pageStr .='<li ><a href="'.$this->urlstr.'?p='.$i.'" target="rframe" >'.$i.'</a></li>';
- }else{
- $this->pageStr .='<li><a href="'.$this->urlstr.'?p='.$i.'" target="rframe" >'.$i.'</a></li>';
- }
- }
- if($this->nowpage !=$this->pageNum){
- $this->pageStr .= '<li><a href="'.$this->urlstr.'?p='.($this->nowpage+1).'" target="rframe" >下一页</a></li>';
- }
- $this->pageStr .='<li><a href="'.$this->urlstr.'?p='.$this->pageNum.'" target="rframe" >末页</a></li>';
- echo $this->pageStr;
- }
- }else if($this->nowpage <= $this->pageNum -2 && $this->nowpage>=5){
- $this->pageStr = '<ul ><li><a href="'.$this->urlstr.'?p=1" target="rframe" >首页</a></li>';
- $this->pageStr .= '<li><a href="'.$this->urlstr.'?p='.($this->nowpage-1).'" target="rframe" >上一页</a></li>';
- for($i=$this->nowpage-2;$i<=$this->nowpage+2;$i++){
- if($i==$this->nowpage){
- $this->pageStr .='<li ><a href="'.$this->urlstr.'?p='.$i.'" target="rframe" >'.$i.'</a></li>';
- }else{
- $this->pageStr .='<li><a href="'.$this->urlstr.'?p='.$i.'" target="rframe" >'.$i.'</a></li>';
- }
- }
- $this->pageStr .= '<li><a href="'.$this->urlstr.'?p='.($this->nowpage+1).'" target="rframe" >下一页</a></li>';
- $this->pageStr .='<li><a href="'.$this->urlstr.'?p='.$this->pageNum.'" target="rframe" >末页</a></li>';
- echo $this->pageStr;
- }else if($this->nowpage>=$this->pageNum-3 && $this->nowpage <=$this->pageNum){
- $this->pageStr = '<ul ><li><a href="'.$this->urlstr.'?p=1" target="rframe" >首页</a></li>';
- $this->pageStr .= '<li><a href="'.$this->urlstr.'?p='.($this->nowpage-1).'" target="rframe" >上一页</a></li>';
- for($i=$this->pageNum-4;$i<=$this->pageNum;$i++){
- if($i==$this->nowpage){
- $this->pageStr .='<li ><a href="'.$this->urlstr.'?p='.$i.'" target="rframe" >'.$i.'</a></li>';
- }else{
- $this->pageStr .='<li><a href="'.$this->urlstr.'?p='.$i.'" target="rframe" >'.$i.'</a></li>';
- }
- }
- if($this->nowpage !=$this->pageNum){
- $this->pageStr .= '<li><a href="'.$this->urlstr.'?p='.($this->nowpage+1).'" target="rframe" >下一页</a></li>';
- }
- $this->pageStr .='<li><a href="'.$this->urlstr.'?p='.$this->pageNum.'" target="rframe" >末页</a></li>';
- echo $this->pageStr;
- }
- }
- }