前言:由于部分查詢資料庫代碼是經過修飾的,是以看起來有點不對勁,但是不影響思路
1.先寫一個生成分頁連結的類
<?php
/**
* Created by PhpStorm.
* User: lxj
* Date: 2017/7/25
* Time: 14:42
*/
class Page{
protected $total;//總的結果數
protected $page_size;//每頁顯示的記錄數
protected $page_now;//目前是第幾頁
protected $url;//跳轉連結時的URL
public function __construct($total, $page_size, $page_now, $url){
$this->total = $total;
$this->page_size = $page_size;
$this->page_now = $page_now;
$this->url = $url;
}
public function createPage(){
$url = $this->url."page=";
//首頁
$html = <<<PAGEHTML
<ul class="pageBar">
<li>
<a href="$this->url">首頁</a>
</li>
PAGEHTML;
//分頁清單,循環生成
$page_count = ceil($this->total / $this->page_size);
for($i = $this->page_now - 5;$i < $this->page_now + 5; $i++){
if($i <= 1 || $i >= $page_count){
continue;
}
$html .= <<<PAGEHTML
<li>
<a href="$url$i">$i</a>
</li>
PAGEHTML;
}
//尾頁
$html .= <<<PAGEHTML
<li>
<a href="$url$page_count">尾頁</a>
</li>
PAGEHTML;
$html .= " 目前為第".$this->page_now."頁,共".$page_count."頁,資料共".$this->total."條";
//傳回分頁連結
return $html;
}
}
2.美化分頁條的樣式
.pageBar{
margin:15px 0 0 25px;
}
.pageBar li{
display: inline-block;
width:35px;
text-align: center;
border:1px solid silver;
background: whitesmoke;
}
.pageBar a:link{
color:steelblue;
display: inline-block;
width:35px;
height:20px;
text-decoration: none;
}
.pageBar a:hover{
color:red;
display: inline-block;
width:35px;
height:20px;
text-decoration: none;
}
.pageBar a:visited{
color:burlywood;
display: inline-block;
width:35px;
height:20px;
text-decoration: none;
}
3.調用分頁類并傳遞參數
//生成分頁連結start
if($_GET['companyName']){
$query_para['companyName'] = $_GET['companyName'];
}
if($_GET['addr_province']){
$query_para['addr_province'] = $_GET['addr_province'];
}
if($_GET['area']){
$query_para['area'] = $_GET['area'];
}
if($_GET['beginDT']){
$query_para['beginDT'] = $_GET['beginDT'];
}
if($_GET['endDT']){
$query_para['endDT'] = $_GET['endDT'];
}if(count($query_para)){
foreach($query_para as $key=>$value){
//如果是頁面參數則不加入傳遞的連結連結中
if($key == 'page'){
continue;
}
$query_string .= $key.'='.$value.'&';
}
}
$nowUrl = "agtProductConsumeList.php?".$query_string;
$pageObj = new Page($this->count,$this->one_page_num,$this->page_now,$nowUrl);
$page_bar = $pageObj->createPage();
//生成分頁連結end
4.根據分頁傳遞的頁數搜尋
public $one_page_num = 5;//每頁顯示記錄數
public $count;//總的記錄數
public $page_now;//目前頁數
protected function getInfo(){
$ws = " where user_id > 10000";
//搜尋結果總數
$rowCount = $this->mysqlObj->sr("select count(*) as num
from user $ws");
$this->count = $rowCount['num'];
$first_row = ($this->page_now - 1) * $this->one_page_num;
$last_row = $this->one_page_num;
$ws .= " LIMIT ".$first_row.",".$last_row;
$result = $this->mysqlObj->sr("select count(*) as num from user $ws");
}