php分页是目前在显示大量结果时所采用的最好的方式,有了下面这些代码的帮助,开发人员可以在多个页面中显示大量的数据,在互联网上,分?页是一般用于搜索结果或是浏览全部信息.
php基本分页,代码如下:
- <?php
-
- $conn = mysql_connect('localhost','dbusername','dbpass') or trigger_error("SQL", E_USER_ERROR);
- $db = mysql_select_db('dbname',$conn) or trigger_error("SQL", E_USER_ERROR);
-
-
- $sql = "SELECT COUNT(*) FROM numbers";
- $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
- $r = mysql_fetch_row($result);
- $numrows = $r[0];
-
-
- $rowsperpage = 10;
-
- $totalpages = ceil($numrows / $rowsperpage);
-
-
- if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
-
- $currentpage = (int) $_GET['currentpage'];
- } else {
-
- $currentpage = 1;
- }
-
-
- if ($currentpage > $totalpages) {
-
- $currentpage = $totalpages;
- }
-
- if ($currentpage < 1) {
-
- $currentpage = 1;
- }
-
-
- $offset = ($currentpage - 1) * $rowsperpage;
-
-
- $sql = "SELECT id, number FROM numbers LIMIT $offset, $rowsperpage";
- $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
-
-
- while ($list = mysql_fetch_assoc($result)) {
-
- echo $list['id'] . " : " . $list['number'] . "<br />";
- }
-
-
-
- $range = 3;
-
-
- if ($currentpage > 1) {
-
- echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
-
- $prevpage = $currentpage - 1;
-
- echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
- }
-
-
- for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
-
- if (($x > 0) && ($x <= $totalpages)) {
-
- if ($x == $currentpage) {
-
- echo " [<b>$x</b>] ";
-
- } else {
-
- echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
- }
- }
- }
-
-
- if ($currentpage != $totalpages) {
-
- $nextpage = $currentpage + 1;
-
- echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";
-
- echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";
- }
-
- ?>
先看一个常用的php分页类,代码如下:
- <?php
-
-
-
- include('config.php');
-
- $tbl_name="";
-
- $adjacents = 3;
-
-
-
-
-
- $query = "SELECT COUNT(*) as num FROM $tbl_name";
- $total_pages = mysql_fetch_array(mysql_query($query));
- $total_pages = $total_pages[num];
-
-
- $targetpage = "filename.php";
- $limit = 2;
- $page = $_GET['page'];
- if($page)
- $start = ($page - 1) * $limit;
- else
- $start = 0;
-
-
- $sql = "SELECT column_name FROM $tbl_name LIMIT $start, $limit";
- $result = mysql_query($sql);
-
-
- if ($page == 0) $page = 1;
- $prev = $page - 1;
- $next = $page + 1;
- $lastpage = ceil($total_pages/$limit);
- $lpm1 = $lastpage - 1;
-
-
-
-
-
- $pagination = "";
- if($lastpage > 1)
- {
- $pagination .= "<div class="pagination">";
-
- if ($page > 1)
- $pagination.= "<a href="$targetpage?page=$prev">? previous</a>";
- else
- $pagination.= "<span class="disabled">? previous</span>";
-
-
- if ($lastpage < 7 + ($adjacents * 2))
- {
- for ($counter = 1; $counter <= $lastpage; $counter++)
- {
- if ($counter == $page)
- $pagination.= "<span class="current">$counter</span>";
- else
- $pagination.= "<a href="$targetpage?page=$counter">$counter</a>";
- }
- }
- elseif($lastpage > 5 + ($adjacents * 2))
- {
-
- if($page < 1 + ($adjacents * 2))
- {
- for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
- {
- if ($counter == $page)
- $pagination.= "<span class="current">$counter</span>";
- else
- $pagination.= "<a href="$targetpage?page=$counter">$counter</a>";
- }
- $pagination.= "...";
- $pagination.= "<a href="$targetpage?page=$lpm1">$lpm1</a>";
- $pagination.= "<a href="$targetpage?page=$lastpage">$lastpage</a>";
- }
-
- elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
- {
- $pagination.= "<a href="$targetpage?page=1">1</a>";
- $pagination.= "<a href="$targetpage?page=2">2</a>";
- $pagination.= "...";
- for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
- {
- if ($counter == $page)
- $pagination.= "<span class="current">$counter</span>";
- else
- $pagination.= "<a href="$targetpage?page=$counter">$counter</a>";
- }
- $pagination.= "...";
- $pagination.= "<a href="$targetpage?page=$lpm1">$lpm1</a>";
- $pagination.= "<a href="$targetpage?page=$lastpage">$lastpage</a>";
- }
-
- else
- {
- $pagination.= "<a href="$targetpage?page=1">1</a>";
- $pagination.= "<a href="$targetpage?page=2">2</a>";
- $pagination.= "...";
- for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
- {
- if ($counter == $page)
- $pagination.= "<span class="current">$counter</span>";
- else
- $pagination.= "<a href="$targetpage?page=$counter">$counter</a>";
- }
- }
- }
-
-
- if ($page < $counter - 1)
- $pagination.= "<a href="$targetpage?page=$next">next ?</a>";
- else
- $pagination.= "<span class="disabled">next ?</span>";
- $pagination.= "</div>n";
- }
- ?>
-
- <?php
- while($row = mysql_fetch_array($result))
- {
-
-
-
- }
- ?>
-
- <?=$pagination?>
实例代码如下:
- <?php
- class PageView{
-
- public $pageNo = 1;
-
- public $pageSize = 20;
-
- public $pageCount = 0;
-
- public $totalNum = 0;
-
- public $offSet = 0;
-
- public $pageData = array();
-
-
- public $hasPrePage = true;
-
- public $hasNextPage = true;
-
- public $pageNoList = array();
-
- public $jsFunction ='jsFunction';
-
-
-
-
-
-
- public function __construct($count=0, $size=20,$pageNo=1,$pageData =array(),$jsFunction='jsFunction'){
-
- $this->totalNum = $count;
- $this->pageSize = $size;
- $this->pageNo = $pageNo;
-
- $this->pageCount = ceil($this->totalNum/$this->pageSize);
- $this->pageCount = ($this->pageCount<=0)?1:$this->pageCount;
-
- $this->pageNo = $this->pageNo == 0 ? 1 : $this->pageNo;
- $this->pageNo = $this->pageNo > $this->pageCount? $this->pageCount : $this->pageNo;
-
-
- $this->offset = ( $this->pageNo - 1 ) * $this->pageSize;
-
- $this->hasPrePage = $this->pageNo == 1 ?false:true;
-
- $this->hasNextPage = $this->pageNo >= $this->pageCount ?false:true;
-
- $this->pageData = $pageData;
- $this->jsFunction = $jsFunction;
-
- }
-
-
-
-
- private function generatePageList(){
- $pageList = array();
- if($this->pageCount <= 9){
- for($i=0;$i<$this->pageCount;$i++){
- array_push($pageList,$i+1);
- }
- }else{
- if($this->pageNo <= 4){
- for($i=0;$i<5;$i++){
- array_push($pageList,$i+1);
- }
- array_push($pageList,-1);
- array_push($pageList,$this->pageCount);
-
- }else if($this->pageNo > $this->pageCount - 4){
- array_push($pageList,1);
-
- array_push($pageList,-1);
- for($i=5;$i>0;$i--){
- array_push($pageList,$this->pageCount - $i+1);
- }
- }else if($this->pageNo > 4 && $this->pageNo <= $this->pageCount - 4){
- array_push($pageList,1);
- array_push($pageList,-1);
-
- array_push($pageList,$this->pageNo -2);
- array_push($pageList,$this->pageNo -1);
- array_push($pageList,$this->pageNo);
- array_push($pageList,$this->pageNo + 1);
- array_push($pageList,$this->pageNo + 2);
-
- array_push($pageList,-1);
- array_push($pageList,$this->pageCount);
-
- }
- }
- return $pageList;
- }
-
-
-
-
-
-
- public function echoPageAsDiv(){
- $pageList = $this->generatePageList();
-
- $pageString ="<div class='pagination'><div class='page-bottom'>";
-
- if(!emptyempty($pageList)){
- if($this->pageCount >1){
- if($this->hasPrePage){
- $pageString = $pageString ."<a class='page-next' href="javascript:" .$this->jsFunction . "(" . ($this->pageNo-1) . ")">上一页</a>";
- }
- foreach ($pageList as $k=>$p){
- if($this->pageNo == $p){
- $pageString = $pageString ."<span class='page-cur'>" . $this->pageNo . "</span>";
- continue;
- }
- if($p == -1){
- $pageString = $pageString ."<span class='page-break'>...</span>";
- continue;
- }
- $pageString = $pageString ."<a href="javascript:" .$this->jsFunction . "(" . $p . ")">" . $p . "</a>";
- }
-
- if($this->hasNextPage){
- $pageString = $pageString ."<a class='page-next' href="javascript:" .$this->jsFunction . "(" . ($this->pageNo+1) . ")">下一页</a>";
- }
-
- }
- }
- $pageString = $pageString .("</div></div>");
- return $pageString;
- }
- }
-
- ?>
css代码如下:
- <style type="text/css">
- <!--
- .pagination {font-family: Tahoma;overflow: hidden; padding-top: 12px; text-align: center;}
- .pagination-tab { margin-bottom: 20px;}
- .pagination a, .pagination .page-cur, .pagination .page-prev_g, .pagination .page-prev, .pagination .page-next, .pagination .page-next_g, .pagination .page-break, .pagination .page-skip {
- display: inline-block;font-family: Tahoma,SimSun,Arial; height: 22px;line-height:22px; margin: 0; min-width: 16px;padding: 0 5px; text-align: center; vertical-align: top; white-space: nowrap;}
- .pagination a, .pagination .page-prev_g, .pagination .page-prev, .pagination .page-next, .pagination .page-next_g, .pagination .page-cur, .pagination .page-break {
- border: 1px solid #ed3d83; color:#e9357d; font-weight:bold;}
- .pagination a:hover { border: 1px solid #ed3d83;text-decoration: none; background-color:#f95f9d; color:#fff;}
- .pagination .page-prev_g, .pagination .page-prev, .pagination .page-next, .pagination .page-next_g { width: 36px; background-image: url(../static/img/page.gif);}
- .pagination .page-prev { background-position: -0px -38px; padding-left: 16px;}
- .pagination .page-prev_g { background-position:0px -59px; padding-left: 16px; color:#cbcbcb; font-weight:normal;}
- .pagination .page-next { background-position: 0px 0px; padding-right: 16px; font-weight:normal;}
- .pagination .page-next_g { background-position: -0px -19px; padding-right: 16px; color:#cbcbcb;}
- .pagination .page-cur {background-color: #f95f9d; border: 1px solid #ed3d83;color: #fff;font-weight: bold;}
- .pagination .page-break {border: medium none; background:none transparent; color:#333;}
-
- -->
- </style>
在php页面中的调用方法,代码如下:
- $pageNo = $_GET['pageNo'];
- if(emptyempty($pageNo)){
- $pageNo = 1;
- }
-
- $pageData = News::getNewsPage($pageNo,$pageSize);
-
- $count = News::getNewsCount();
-
- $p = new PageView($count['0']['TOTAL'],$pageSize,$pageNo,$pageData);
-
- $pageViewString = $p->echoPageAsDiv();
|