php本身是沒(méi)有分頁(yè)概念的,分頁(yè)是URL傳參,然后通過(guò)mysql查詢(xún)語(yǔ)句到數(shù)據(jù)庫(kù)獲取數(shù)據(jù),然后實(shí)現(xiàn)的分頁(yè),url上的參數(shù),通過(guò)PHP的$_GET都是可以獲取到的。
文成ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書(shū)未來(lái)市場(chǎng)廣闊!成為成都創(chuàng)新互聯(lián)的ssl證書(shū)銷(xiāo)售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話(huà)聯(lián)系或者加微信:028-86922220(備注:SSL證書(shū)合作)期待與您的合作!
現(xiàn)在市面上的PHP框架基本都有PHP分頁(yè)類(lèi),參照文檔直接調(diào)用就好了,如果想看實(shí)現(xiàn)過(guò)程,可以去下載一個(gè)TP框架,然后打開(kāi)里面的分頁(yè)類(lèi)查看里面的源代碼。
原理:
1.設(shè)定每頁(yè)顯示數(shù)
$num = 15;
2.獲取數(shù)據(jù)總數(shù)計(jì)算總頁(yè)數(shù)
3.通過(guò)get方法獲取頁(yè)碼,無(wú)獲取值頁(yè)碼為1,超過(guò)最大頁(yè)數(shù)為最大頁(yè)面數(shù)
$page = $_GET['page'] ? $_GET['page'] : 1;
4.通過(guò)頁(yè)碼來(lái)計(jì)算當(dāng)前顯示起始記錄
$start = $page*$num-1;
5.通過(guò)sql語(yǔ)句查詢(xún)
query("select * from tablename limit $start,$num");
然后輸出就行了
頁(yè)面上可以循環(huán)輸出1~總頁(yè)數(shù) 這些數(shù)字來(lái)實(shí)現(xiàn)翻頁(yè),也可以用其他方法
比較方便的方法是,可以去找一個(gè)分頁(yè)類(lèi)
class Page
{
// 起始行數(shù)
public $firstRow ;
// 列表每頁(yè)顯示行數(shù)
public $listRows ;
// 頁(yè)數(shù)跳轉(zhuǎn)時(shí)要帶的參數(shù)
public $parameter ;
// 分頁(yè)總頁(yè)面數(shù)
protected $totalPages ;
// 總行數(shù)
protected $totalRows ;
// 當(dāng)前頁(yè)數(shù)
protected $nowPage ;
// 分頁(yè)的欄的總頁(yè)數(shù)
protected $coolPages ;
// 分頁(yè)欄每頁(yè)顯示的頁(yè)數(shù)
protected $rollPage ;
// 分頁(yè)參數(shù)
public $p;
// 分頁(yè)顯示定制
protected $config = array(
'header'='條記錄',
'prev'='上一頁(yè)',
'next'='下一頁(yè)',
'first'='第一頁(yè)',
'last'='最后一頁(yè)',
'theme'=' %totalRow% %header% %nowPage%/%totalPage% 頁(yè) %upPage% %downPage% %first% %prePage% %linkPage% %nextPage% %end%'
);
/**
+----------------------------------------------------------
* 架構(gòu)函數(shù)
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param array $totalRows 總的記錄數(shù)
* @param array $listRows 每頁(yè)顯示記錄數(shù)
* @param array $parameter 分頁(yè)跳轉(zhuǎn)的參數(shù)
+----------------------------------------------------------
*/
public function __construct($totalRows,$listRows,$p='p',$rollPage=5,$parameter='') {
$this-p=$p;
$this-totalRows = $totalRows;
$this-parameter = $parameter;
$this-rollPage = $rollPage;
$this-listRows = !empty($listRows)?$listRows:15;
$this-totalPages = ceil($this-totalRows/$this-listRows); //總頁(yè)數(shù)
$this-coolPages = ceil($this-totalPages/$this-rollPage);
$this-nowPage = !empty($_GET[$this-p])?$_GET[$this-p]:1;
if(!empty($this-totalPages) $this-nowPage$this-totalPages) {
$this-nowPage = $this-totalPages;
}
$this-firstRow = $this-listRows*($this-nowPage-1);
}
public function setConfig($name,$value) {
if(isset($this-config[$name])) {
$this-config[$name] = $value;
}
}
/**
+----------------------------------------------------------
* 分頁(yè)顯示輸出
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
*/
public function show() {
if(0 == $this-totalRows) return '';
$nowCoolPage = ceil($this-nowPage/$this-rollPage);
$url = $_SERVER['REQUEST_URI'].(strpos($_SERVER['REQUEST_URI'],'?')?'':"?").$this-parameter;
$parse = parse_url($url);
if(isset($parse['query'])) {
parse_str($parse['query'],$params);
unset($params[$this-p]);
$url = $parse['path'].'?'.http_build_query($params);
}
//上下翻頁(yè)字符串
$upRow = $this-nowPage-1;
$downRow = $this-nowPage+1;
if ($upRow0){
$upPage="a href='".$url."".$this-p."=$upRow'".$this-config['prev']."/a";
}else{
$upPage="";
}
if ($downRow = $this-totalPages){
$downPage="a href='".$url."".$this-p."=$downRow'".$this-config['next']."/a";
}else{
$downPage="";
}
//
if($nowCoolPage == 1){
$theFirst = "";
$prePage = "";
}else{
$preRow = $this-nowPage-$this-rollPage;
$prePage = "a href='".$url."".$this-p."=$preRow' 上".$this-rollPage."頁(yè)/a";
$theFirst = "a href='".$url."".$this-p."=1' ".$this-config['first']."/a";
}
if($nowCoolPage == $this-coolPages){
$nextPage = "";
$theEnd="";
}else{
$nextRow = $this-nowPage+$this-rollPage;
$theEndRow = $this-totalPages;
$nextPage = "a href='".$url."".$this-p."=$nextRow' 下".$this-rollPage."頁(yè)/a";
$theEnd = "a href='".$url."".$this-p."=$theEndRow' ".$this-config['last']."/a";
}
// 1 2 3 4 5
$linkPage = "";
for($i=1;$i=$this-rollPage;$i++){
$page=($nowCoolPage-1)*$this-rollPage+$i;
if($page!=$this-nowPage){
if($page=$this-totalPages){
$linkPage .= "?a href='".$url."".$this-p."=$page'?".$page."?/a";
}else{
break;
}
}else{
if($this-totalPages != 1){
$linkPage .= "?span class='current'".$page."/span";
}
}
}
$pageStr = str_replace(
array('%header%','%nowPage%','%totalRow%','%totalPage%','%upPage%','%downPage%','%first%','%prePage%','%linkPage%','%nextPage%','%end%'),
array($this-config['header'],$this-nowPage,$this-totalRows,$this-totalPages,$upPage,$downPage,$theFirst,$prePage,$linkPage,$nextPage,$theEnd),$this-config['theme']);
return $pageStr;
}
}
具體調(diào)用方法
$count是總記錄數(shù)
15是當(dāng)前顯示數(shù)
$p=new Page($count,15);
$p-setConfig('header', '個(gè)項(xiàng)目'); //可以設(shè)置變量名稱(chēng),即輸出的名稱(chēng)
$page=$p-show(); //生成html page分頁(yè)html
總的流程是
先讀取出總的記錄數(shù)
,然后調(diào)用上面的分頁(yè)類(lèi)
在分頁(yè)類(lèi)里面輸出2個(gè)變量一個(gè)是當(dāng)前分頁(yè)讀取數(shù)據(jù)的開(kāi)始,和偏移量
limit($p-listRows,$p-firstRow)
然后執(zhí)行分頁(yè)sql語(yǔ)句
$page=$p-show();
page就是標(biāo)準(zhǔn)分頁(yè)html
1、前言
分頁(yè)顯示是一種非常常見(jiàn)的瀏覽和顯示大量數(shù)據(jù)的方法,屬于web編程中最常處理的事件之一。對(duì)于web編程的老手來(lái)說(shuō),編寫(xiě)這種代碼實(shí)在是和呼吸一樣自然,但是對(duì)于初學(xué)者來(lái)說(shuō),常常對(duì)這個(gè)問(wèn)題摸不著頭緒,因此特地撰寫(xiě)此文對(duì)這個(gè)問(wèn)題進(jìn)行詳細(xì)的講解,力求讓看完這篇文章的朋友在看完以后對(duì)于分頁(yè)顯示的原理和實(shí)現(xiàn)方法有所了解。本文適合初學(xué)者閱讀,所有示例代碼均使用php編寫(xiě)。
2、原理
所謂分頁(yè)顯示,也就是將數(shù)據(jù)庫(kù)中的結(jié)果集人為的分成一段一段的來(lái)顯示,這里需要兩個(gè)初始的參數(shù):
每頁(yè)多少條記錄($PageSize)?
當(dāng)前是第幾頁(yè)($CurrentPageID)?
現(xiàn)在只要再給我一個(gè)結(jié)果集,我就可以顯示某段特定的結(jié)果出來(lái)。
至于其他的參數(shù),比如:上一頁(yè)($PreviousPageID)、下一頁(yè)($NextPageID)、總頁(yè)數(shù)($numPages)等等,都可以根據(jù)前邊這幾個(gè)東西得到。
以mysql數(shù)據(jù)庫(kù)為例,如果要從表內(nèi)截取某段內(nèi)容,sql語(yǔ)句可以用:select * from table limit offset, rows??纯聪旅嬉唤Msql語(yǔ)句,嘗試一下發(fā)現(xiàn)其中的規(guī)率。
前10條記錄:select * from table limit 0,10
第11至20條記錄:select * from table limit 10,10
第21至30條記錄:select * from table limit 20,10
……
這一組sql語(yǔ)句其實(shí)就是當(dāng)$PageSize=10的時(shí)候取表內(nèi)每一頁(yè)數(shù)據(jù)的sql語(yǔ)句,我們可以總結(jié)出這樣一個(gè)模板:
select * from table limit ($CurrentPageID - 1) * $PageSize, $PageSize
拿這個(gè)模板代入對(duì)應(yīng)的值和上邊那一組sql語(yǔ)句對(duì)照一下看看是不是那么回事。搞定了最重要的如何獲取數(shù)據(jù)的問(wèn)題以后,剩下的就僅僅是傳遞參數(shù),構(gòu)造合適的sql語(yǔ)句然后使用php從數(shù)據(jù)庫(kù)內(nèi)獲取數(shù)據(jù)并顯示了。以下我將用具體代碼加以說(shuō)明。
3、簡(jiǎn)單代碼
請(qǐng)?jiān)敿?xì)閱讀以下代碼,自己調(diào)試運(yùn)行一次,最好把它修改一次,加上自己的功能,比如搜索等等。
?php
// 建立數(shù)據(jù)庫(kù)連接
$link = mysql_connect("localhost", "mysql_user", "mysql_password")
or die("Could not connect: " . mysql_error());
// 獲取當(dāng)前頁(yè)數(shù)
if( isset($_GET['page']) ){
$page = intval( $_GET['page'] );
}
else{
$page = 1;
}
// 每頁(yè)數(shù)量
$PageSize = 10;
// 獲取總數(shù)據(jù)量
$sql = "select count(*) as amount from table";
$result = mysql_query($sql);
$row = mysql_fetch_row($result);
$amount = $row['amount'];
// 記算總共有多少頁(yè)
if( $amount ){
if( $amount $page_size ) //如果總數(shù)據(jù)量小于$PageSize,那么只有一頁(yè)
if( $amount % $page_size ){ //取總數(shù)據(jù)量除以每頁(yè)數(shù)的余數(shù)
$page_count = (int)($amount / $page_size) + 1; //如果有余數(shù),則頁(yè)數(shù)等于總數(shù)據(jù)量除以每頁(yè)數(shù)的結(jié)果取整再加一
}else{
$page_count = $amount / $page_size; //如果沒(méi)有余數(shù),則頁(yè)數(shù)等于總數(shù)據(jù)量除以每頁(yè)數(shù)的結(jié)果
}
}
else{
$page_count = 0;
}
// 翻頁(yè)鏈接
$page_string = '';
if( $page == 1 ){
$page_string .= '第一頁(yè)|上一頁(yè)|';
}
else{
$page_string .= 'a href="/?page=1";第一頁(yè)/a|a href="/?page='."($page-1).'上一頁(yè)/a|';
}
if( ($page == $page_count) || ($page_count == 0) ){
$page_string .= '下一頁(yè)|尾頁(yè)';
}
else{
$page_string .= 'a href="/?page='."($page+1).'下一頁(yè)/a|a href="/?page='."$page_count.'尾頁(yè)/a';
}
// 獲取數(shù)據(jù),以二維數(shù)組格式返回結(jié)果
if( $amount ){
$sql = "select * from table order by id desc limit ". ($page-1)*$page_size .", $page_size";
$result = mysql_query($sql);
while ( $row = mysql_fetch_row($result) ){
$rowset[] = $row;
}
}else{
$rowset = array();
}
// 沒(méi)有包含顯示結(jié)果的代碼,那不在討論范圍,只要用foreach就可以很簡(jiǎn)單的用得到的二維數(shù)組來(lái)顯示結(jié)果
?
4、OO風(fēng)格代碼
以下代碼中的數(shù)據(jù)庫(kù)連接是使用的pear db類(lèi)進(jìn)行處理
?php
// FileName: Pager.class.php
// 分頁(yè)類(lèi),這個(gè)類(lèi)僅僅用于處理數(shù)據(jù)結(jié)構(gòu),不負(fù)責(zé)處理顯示的工作
Class Pager
{
var $PageSize; //每頁(yè)的數(shù)量
var $CurrentPageID; //當(dāng)前的頁(yè)數(shù)
var $NextPageID; //下一頁(yè)
var $PreviousPageID; //上一頁(yè)
var $numPages; //總頁(yè)數(shù)
var $numItems; //總記錄數(shù)
var $isFirstPage; //是否第一頁(yè)
var $isLastPage; //是否最后一頁(yè)
var $sql; //sql查詢(xún)語(yǔ)句
function Pager($option)
{
global $db;
$this-_setOptions($option);
// 總條數(shù)
if ( !isset($this-numItems) )
{
$res = $db-query($this-sql);
$this-numItems = $res-numRows();
}
// 總頁(yè)數(shù)
if ( $this-numItems 0 )
{
if ( $this-numItems $this-PageSize )
if ( $this-numItems % $this-PageSize )
{
$this-numPages= (int)($this-numItems / $this-PageSize) + 1;
}
else
{
$this-numPages = $this-numItems / $this-PageSize;
}
}
else
{
$this-numPages = 0;
}
switch ( $this-CurrentPageID )
{
case $this-numPages == 1:
$this-isFirstPage = true;
$this-isLastPage = true;
break;
case 1:
$this-isFirstPage = true;
$this-isLastPage = false;
break;
case $this-numPages:
$this-isFirstPage = false;
$this-isLastPage = true;
break;
default:
$this-isFirstPage = false;
$this-isLastPage = false;
}
if ( $this-numPages 1 )
{
if ( !$this-isLastPage )
if ( !$this-isFirstPage )
}
return true;
}
/***
*
* 返回結(jié)果集的數(shù)據(jù)庫(kù)連接
* 在結(jié)果集比較大的時(shí)候可以直接使用這個(gè)方法獲得數(shù)據(jù)庫(kù)連接,然后在類(lèi)之外遍歷,這樣開(kāi)銷(xiāo)較小
* 如果結(jié)果集不是很大,可以直接使用getPageData的方式獲取二維數(shù)組格式的結(jié)果
* getPageData方法也是調(diào)用本方法來(lái)獲取結(jié)果的
*
***/
function getDataLink()
{
if ( $this-numItems )
{
global $db;
$PageID = $this-CurrentPageID;
$from = ($PageID - 1)*$this-PageSize;
$count = $this-PageSize;
$link = $db-limitQuery($this-sql, $from, $count); //使用Pear DB::limitQuery方法保證數(shù)據(jù)庫(kù)兼容性
return $link;
}
else
{
return false;
}
}
/***
*
* 以二維數(shù)組的格式返回結(jié)果集
*
***/
function getPageData()
{
if ( $this-numItems )
{
if ( $res = $this-getDataLink() )
{
if ( $res-numRows() )
{
while ( $row = $res-fetchRow() )
{
$result[] = $row;
}
}
else
{
$result = array();
}
return $result;
}
else
{
return false;
}
}
else
{
return false;
}
}
function _setOptions($option)
{
$allow_options = array(
'PageSize',
'CurrentPageID',
'sql',
'numItems'
);
foreach ( $option as $key = $value )
{
if ( in_array($key, $allow_options) ($value != null) )
{
$this-$key = $value;
}
}
return true;
}
}
?
?php
// FileName: test_pager.php
// 這是一段簡(jiǎn)單的示例代碼,前邊省略了使用pear db類(lèi)建立數(shù)據(jù)庫(kù)連接的代碼
require "Pager.class.php";
if ( isset($_GET['page']) )
{
$page = (int)$_GET['page'];
}
else
{
$page = 1;
}
$sql = "select * from table order by id";
$pager_option = array(
"sql" = $sql,
"PageSize" = 10,
"CurrentPageID" = $page
);
if ( isset($_GET['numItems']) )
{
$pager_option['numItems'] = (int)$_GET['numItems'];
}
$pager = @new Pager($pager_option);
$data = $pager-getPageData();
if ( $pager-isFirstPage )
{
$turnover = "首頁(yè)|上一頁(yè)|";
}
else
{
$turnover = "a href='?page=1numItems=".$pager-numItems."'首頁(yè)/a|a href="/?page=".$pager-PreviousPageID."numItems=".$pager-numItems."'上一頁(yè)/a|";
}
if ( $pager-isLastPage )
{
$turnover .= "下一頁(yè)|尾頁(yè)";
}
else
{
$turnover .= "a href="/?page=".$pager-NextPageID."numItems=".$pager-numItems."'下一頁(yè)/a|a href="/?page=".$pager-numPages."numItems=".$pager-numItems."'尾頁(yè)/a";
}
?
需要說(shuō)明的地方有兩個(gè):
這個(gè)類(lèi)僅僅處理數(shù)據(jù),并不負(fù)責(zé)處理顯示,因?yàn)槲矣X(jué)得將數(shù)據(jù)的處理和結(jié)果的顯示都放到一個(gè)類(lèi)里邊實(shí)在是有些勉強(qiáng)。顯示的時(shí)候情況和要求多變,不如自己根據(jù)類(lèi)給出的結(jié)果處理,更好的方法是根據(jù)這個(gè)Pager類(lèi)繼承一個(gè)自己的子類(lèi)來(lái)顯示不同的分頁(yè),比如顯示用戶(hù)分頁(yè)列表可以:
?php
Class MemberPager extends Pager
{
function showMemberList()
{
global $db;
$data = $this-getPageData();
// 顯示結(jié)果的代碼
// ......
}
}
/// 調(diào)用
if ( isset($_GET['page']) )
{
$page = (int)$_GET['page'];
}
else
{
$page = 1;
}
$sql = "select * from members order by id";
$pager_option = array(
"sql" = $sql,
"PageSize" = 10,
"CurrentPageID" = $page
);
if ( isset($_GET['numItems']) )
{
$pager_option['numItems'] = (int)$_GET['numItems'];
}
$pager = @new MemberPager($pager_option);
$pager-showMemberList();
?
第二個(gè)需要說(shuō)明的地方就是不同數(shù)據(jù)庫(kù)的兼容性,在不同的數(shù)據(jù)庫(kù)里截獲一段結(jié)果的寫(xiě)法是不一樣的。
mysql: select * from table limit offset, rows
pgsql: select * from table limit m offset n
......
所以要在類(lèi)里邊獲取結(jié)果的時(shí)候需要使用pear db類(lèi)的limitQuery方法。
ok,寫(xiě)完收功,希望花時(shí)間看完這些文字的你不覺(jué)得是浪費(fèi)了時(shí)間。
回答者
另外,虛機(jī)團(tuán)上產(chǎn)品團(tuán)購(gòu),超級(jí)便宜
當(dāng)前文章:php從最新數(shù)據(jù)開(kāi)始分頁(yè),php分頁(yè)查詢(xún)功能實(shí)現(xiàn)
文章轉(zhuǎn)載:http://chinadenli.net/article4/dsshgie.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、電子商務(wù)、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站排名、ChatGPT、
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)