亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

PHP分頁顯示制作詳細(xì)講解

瀏覽:34日期:2023-12-25 17:53:06

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??纯聪旅嬉唤Msql語句,嘗試一下發(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 ){ $page_count = 1; };;;;//如果總數(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 ){ $this->numPages = 1; } 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 ) { $this->NextPageID = $this->CurrentPageID + 1; } if ( !$this->isFirstPage ) { $this->PreviousPageID = $this->CurrentPageID - 1; } } 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='http://www.aoyou183.cn/bcjs/?page=1&numItems='.$pager->numItems.''>首頁</a>|<a href='http://www.aoyou183.cn/bcjs/?page='.$pager->PreviousPageID.'&numItems='.$pager->numItems.''>上一頁</a>|';} if ( $pager->isLastPage ){ $turnover .= '下一頁|尾頁';}else{ $turnover .= '<a href='http://www.aoyou183.cn/bcjs/?page='.$pager->NextPageID.'&numItems='.$pager->numItems.''>下一頁</a>|<a href='http://www.aoyou183.cn/bcjs/?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è)自己的子類來顯示不同的分頁,比如顯示用戶分頁列表可以:

<?phpClass 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, rowspgsql: select * from table limit m offset n......所以要在類里邊獲取結(jié)果的時(shí)候需要使用pear db類的limitQuery方法。

ok,寫完收功,希望花時(shí)間看完這些文字的你不覺得是浪費(fèi)了時(shí)間。

標(biāo)簽: PHP
主站蜘蛛池模板: 国产日韩亚洲欧洲一区二区三区 | 69久成人做爰视频 | 久久在线精品视频 | 国产一区二区在线观看麻豆 | 国产高清在线视频 | 在线免费黄色网址 | 国产在线一区二区三区四区 | 国产乱码精品一区二区三区卡 | 国产乱码精品一区二区三区四川 | 1024视频在线 | 免费a在线 | 欧洲男女啪啪免费观看 | 99爱在线视频这里只有精品 | 国产欧美综合在线观看第七页 | 免费中日高清无专码有限公司 | 欧美区国产区 | 久久99国产一区二区三区 | 免费在线黄色网址 | 亚洲精品国产男人的天堂 | 极品精品国产超清自在线观看 | 九九热线精品视频6一 | 国产日韩欧美在线观看播放 | 午夜91| 黄色视屏免费看 | 青青操视频在线免费观看 | 日韩高清在线免费观看 | 久久国产香蕉视频 | 婷婷五月色综合 | 国产高清视频网站 | 欧美做爱毛片 | 国产久草视频在线 | 亚洲91色 | 免费在线一级毛片 | 亚洲妇熟xxxxx妇色黄 | 国产高清区| 久久免费视频观看 | 久久久亚洲国产精品主播 | 一区在线看 | 亚洲香蕉综合在人在线时看 | 欧美三级视频在线观看 | 亚洲国产美女精品久久久久 |