php分页实例程序函数
在php中分页是我们开发中必须要用到的东西,但在觉得在php分页比在asp中方便了很多,下面我来给各位同学详细介绍人几个不错的php分页类吧,这是我自己写的一个php分页实例,代码如下:
- <html>
- <head>
- <title>简单的PHP分页程序</title>
- </head>
- <body>
- <?php
-
- $linkID=@mysql_connect("phpfensi.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%" border="1">
- <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{
-
-
-
-
- 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;
- }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);
-
- }
-
-
-
-
-
-
-
- public function showpage(){
- if($this->pageNum >=1 && $this->nowpage <5 ){
-
- if( $this->pageNum <=5){
-
- $this->pageStr = '<ul id="pages"><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 id="spages"><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 id="pages"><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 id="spages"><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 id="pages"><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 id="spages"><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 id="pages"><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 id="spages"><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;
-
- }
- }
- }