分页是目前在显示大量结果时所采用的最好的方式。有了下面这些代码的帮助,开发人员可以在多个页面中显示大量的数据。在互联网上,分页是一般用于搜索结果或是浏览全部信息
php基本分页
| 代码如下 | 复制代码 |
|
// database connection info // find out how many rows are in the table // number of rows to show per page // get the current page or set a default 立即学习“PHP免费学习笔记(深入)”; // if current page is greater than total pages... // the offset of the list, based on current page // get the info from the db // while there are rows to be fetched... /****** build the pagination links ******/ // if not on page 1, don't show back links // loop to show links to range of pages around current page // if not on last page, show forward and last page links | |
先看一个常用的php分页类
| 代码如下 | 复制代码 |
|
/* $tbl_name=""; //your table name "; n"; //previous button if ($page > 1) $pagination.= "� previous"; else $pagination.= "� previous"; //pages if ($lastpage { for ($counter = 1; $counter { if ($counter == $page) $pagination.= "$counter"; else $pagination.= "$counter"; } } elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some { //close to beginning; only hide later pages if($page { for ($counter = 1; $counter { if ($counter == $page) $pagination.= "$counter"; else $pagination.= "$counter"; } $pagination.= "..."; $pagination.= "$lpm1"; $pagination.= "$lastpage"; } //in middle; hide some front and some back elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)) { $pagination.= "1"; $pagination.= "2"; $pagination.= "..."; for ($counter = $page - $adjacents; $counter { if ($counter == $page) $pagination.= "$counter"; else $pagination.= "$counter"; } $pagination.= "..."; $pagination.= "$lpm1"; $pagination.= "$lastpage"; } //close to end; only hide early pages else { $pagination.= "1"; $pagination.= "2"; $pagination.= "..."; for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter { if ($counter == $page) $pagination.= "$counter"; else $pagination.= "$counter"; } } } //next button if ($page $pagination.= "next �"; else $pagination.= "next �"; $pagination.= " } ?> while($row = mysql_fetch_array($result)) =$pagination?> | |
实例
| 代码如下 | 复制代码 |
|
class PageView{ $this->totalNum = $count;//总记录数 $this->hasNextPage = $this->pageNo >= $this->pageCount ?false:true; }else if($this->pageNo > $this->pageCount - 4){ /*** "; if(!empty($pageList)){ if($this->pageCount >1){ if($this->hasPrePage){ $pageString = $pageString ."jsFunction . "(" . ($this->pageNo-1) . ")">上一页"; } foreach ($pageList as $k=>$p){ if($this->pageNo == $p){ $pageString = $pageString ."" . $this->pageNo . ""; continue; } if($p == -1){ $pageString = $pageString ."..."; continue; } $pageString = $pageString ."jsFunction . "(" . $p . ")">" . $p . ""; } if($this->hasNextPage){ $pageString = $pageString ."jsFunction . "(" . ($this->pageNo+1) . ")">下一页"; } } } $pageString = $pageString .(" return $pageString; } } ?> | |
css代码
| 代码如下 | 复制代码 |
|
--> | |
在php页面中的调用方法
| 代码如下 | 复制代码 |
$pageNo = $_GET['pageNo']; | |
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号