国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

? php教程 PHP源碼 經(jīng)典php分頁(yè)代碼與分頁(yè)原理(1/3)

經(jīng)典php分頁(yè)代碼與分頁(yè)原理(1/3)

Jun 08, 2016 pm 05:26 PM
nbsp page quot this

<script>ec(2);</script>

經(jīng)典php教程分頁(yè)代碼與分頁(yè)原理
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)行一次,最好把它修改一次,加上自己的功能,比如搜索等等。

// 建立數(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 ? 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 .= '|上一頁(yè)|';
}
if( ($page == $page_count) || ($page_count == 0) ){
? $page_string .= '下一頁(yè)|尾頁(yè)';
}
else{
? $page_string .= '下一頁(yè)|尾頁(yè)';
}
// 獲取數(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)行處理

// 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 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;
??? }

首頁(yè) 1 2 3 末頁(yè)
? ????? ??
? ?? ??? ????? ???? ??? ??????, ???? ?????? ????. ? ???? ?? ???? ?? ??? ?? ????. ???? ??? ???? ???? ??? ?? admin@php.cn?? ?????.

? AI ??

Undresser.AI Undress

Undresser.AI Undress

???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover

AI Clothes Remover

???? ?? ???? ??? AI ?????.

Video Face Swap

Video Face Swap

??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

???

??? ??

???++7.3.1

???++7.3.1

???? ?? ?? ?? ???

SublimeText3 ??? ??

SublimeText3 ??? ??

??? ??, ???? ?? ????.

???? 13.0.1 ???

???? 13.0.1 ???

??? PHP ?? ?? ??

???? CS6

???? CS6

??? ? ?? ??

SublimeText3 Mac ??

SublimeText3 Mac ??

? ??? ?? ?? ?????(SublimeText3)

???

??? ??

??? ????
1601
29
PHP ????
1502
276
???
Windows 11?? ? ??? ??? ???? ??: ?? ? ?? ?? Windows 11?? ? ??? ??? ???? ??: ?? ? ?? ?? Sep 22, 2023 am 11:37 AM

Windows 11? ???? ??? ???? ??? ??????. ???? ?????? ?? ? ???? ?? ??? ?? ??? ????? ??? ? ????. ? ?????? Windows ?? ???? ??? ???? ???? ??? ??? ? ??? ?? ??? ??? ?????. ? ??? ??? ???? ??? ?????? +? ?? ?? ?? ???. Windows?? ???? ???? ?? ??? ?????. ?? ?? ? ??? ?? ? 11" Width="643" Height="500" > ?? ??? ? ? ???? ?? ?? ?? ??? ?? ?? ?? ???? ?????. ?? ?? ? ?? ???? ?? ??? ????? ?? ??? ?? ???? ?? ??? ????? ?? ??? ?? ???? ?? ??? ???.

?? ??: ???? PIN ??? ?????. ?? ??: ???? PIN ??? ?????. Oct 04, 2023 pm 05:45 PM

??? ??? "??? ???? PIN ??? ?????"?? ???? ?????. ?? ?? ??? ??? ? ?? ?? ?? ?? ??? ???? ????? PIN ?? ??? ??? ?? ?????. ??? ?? ??? ???? Windows? ???? ?? ?????? ?? ???? ???? ????. ?? ?? ?? ????. ??? ??? ???? ???? ?? ??? ???? ?????. ???? Windows 11?? PIN? ????? ???? ??? ?????? ??? ??? ??? ???? ?? ? ???? ?? ???? ?? ?? ?? ?????. ??? ????? ???? ??? ? ? ????! ?? ?? ??? ?? ?? ???? ??? ????? ?? ?? ??? ??? ? ????. ?? ??

Windows 11?? ?? ??? ??? ???? ??? ?????? Windows 11?? ?? ??? ??? ???? ??? ?????? Sep 14, 2023 pm 03:33 PM

????? Windows 11? ?? ??? ??? ??? ???/?? ??? ?? ????. ??? ??? ???? ??? ? ????. ? ?????? ?? ???? ???? ??? ????? ????? ????? ??? ? ?? ??? ?? ??? ??? ?????. ?? ?? ??? ?? ?? ??? ??? ??? ? ????? ?, ?? ?? ???? ?? ?? ?? ??? ??? ????? ????? ???? ???? ??? ?? ?? ??? ??? ??? ? ????. ??? ??? ????? ?? ???? ?????. Windows 11?? ?? ??? ??? ???? ??? ?????? 1. ?? ?? ???? +? ?? ?? ?? ???. Windows"?? ??"?? ??? ??

Windows 11?? ?? ??? ??? ?? ??? ??? ?? ?????? ?? Windows 11?? ?? ??? ??? ?? ??? ??? ?? ?????? ?? Sep 15, 2023 pm 03:57 PM

?? ??? ???? ???? ?? ??? ??? ???? ??? ???? ? ?? ????. ? ?? ?? ??? ?? ???? ????? ???? ??? ??? ?? ? ? ??? ?? ????. ? ?? ??? ? ?? ??? ???? ????? ????. ??? ??? ???? ?? ? ?? ??? ?? ??? ?????? ??? ????????. ??? ???? ??? ?? ??? ? ?? ?? ??? ??? ?? ???? ? ????. Windows 11?? ?? ??? ??? ?? ??? ????? ??? ?????? 1. ?? ?? ???? ?? ??? ??? ?????. Windows??? ???? ???? ??? ?????. ?? ??? ??? ?????. ?? ??? ???? ?? ???? ??? ?????. "?? ??"? ?????.

Windows 11/10 ??? OOBELANGUAGE ?? ?? Windows 11/10 ??? OOBELANGUAGE ?? ?? Jul 16, 2023 pm 03:29 PM

Windows Installer ???? "OOBELANGUAGE" ?? ?? "??? ??????."? ?????? ??? ??? ?? Windows ??? ???? ??? ????. OOBE? ?? ?? ??? ??? ?????. ?? ????? ? ? ??? ?? OOBE ?? ??? ??? ?????. ??? ??? ????. OOBE ?? ???? ?????? ???? ? ??? ??? ? ????. ?? ?? – 1. OOBE ? ??? ?? “?? ??” ??? ?????. ??? ? ??? ?? ?? ????? ?????. 2. ?? ??? ???? ???? ?? ?????. ???? ?? ??? ? OOBE? ????? ???. 3. ????? ??? ??? ????. ???? ???? OOBE? ?? ??? ?????.

Windows 11?? ??? ???? 10?? ?? Windows 11?? ??? ???? 10?? ?? Dec 18, 2023 pm 02:21 PM

?? ??? ?? ??? ??? ??? ? ???? ????, ?? ??? ??? ? ? ?? ?????. ?? ??? ???, ???? ???, ???? ?? ????? ?? ? ??? ???. ??? ??? ?? ?? ??? ??? ? ???, ?? ??? UI ??? ??? Windows 11??? ?? ?????. ??? ???? ? ??? ?? ?? Windows 11?? ??? ???? ?? ??? ??? ????. Windows 11?? ??? ???? ?? [10?? ??] ?? ??? ???? ?? ??? ???? Windows 11?? ??? ??? ? ????. ???? ?? ???? ???? ???? ???? ???? ?????. ????. ?? 1: ?? ?? ?? ?? ??? ???? ? ????.

Windows 11? ????? ?? ?? ??? Windows 11? ????? ?? ?? ??? Sep 19, 2023 pm 06:45 PM

Windows 11? ????? ?? ??? ???? ?? ??? ?? ?? ???? ??? ????. ? ???? ???? ??? ??, ?? ???? ???? ??? ????. ??? ??? ?? ??? ????? ??? ??? ?????. ??? ?? ?? ???? ???? ??? ?? ??? ?? ? ???? ???? ? ???? ??? ??? ??? ????? ?? ??? ???? ??? ??? ???. Custom Zoom? ??: ??? ???? ?? ??? ????? ??? ?????. ? ?? ???? ? ?? ?? ? ? ??? ?????. ?? ??? ? ?? ?????? ???? ??? ?? ?? ???? ??? ? ????. ??? ????? ??? ????? ? ??? ? ? ????. ?? ?? ??? ??? ? ????? ??? ? ????. ??? 11? ???? ??

Windows Server?? ??? ?? ?? 0xc004f069? ???? ?? Windows Server?? ??? ?? ?? 0xc004f069? ???? ?? Jul 22, 2023 am 09:49 AM

Windows? ?? ?? ?????? ??? ? ?? ?? 0xc004f069? ??? ?? ???? ???? ??? ????. ??? ????? ????? ?????? Windows Server? ???? ?? ?? ????? ? ??? ??? ? ????. ??? ?? ??? ???? ??? ???? ??? ?? ??? ?? ?? ???? ???? ??? ??????. ?? ?? - ?? ???? ??? ?? ????. ?? ?? ???? ?? ??????. Windows ?? ?? ????? ???? ?? ?????. ?? 1 – ????? ??? cmd ????? Windows Server Edition ???? ??????. 1?? – Windows Server ?? ?? ?? ???? ?? W ??? ???? ???.

See all articles