php本身是沒有分頁概念的,分頁是URL傳參,然后通過mysql查詢語句到數(shù)據(jù)庫獲取數(shù)據(jù),然后實(shí)現(xiàn)的分頁,url上的參數(shù),通過PHP的$_GET都是可以獲取到的。

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