經(jīng)典php分頁(yè)代碼與分頁(yè)原理(1/3)
Jun 08, 2016 pm 05:26 PM經(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類進(jìn)行處理
// filename: pager.class.php
// 分頁(yè)類,這個(gè)類僅僅用于處理數(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查詢語(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;
??? }

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Windows 11 brings fresh and elegant design to the forefront; the modern interface allows you to personalize and change the finest details, such as window borders. In this guide, we'll discuss step-by-step instructions to help you create an environment that reflects your style in the Windows operating system. How to change window border settings? Press + to open the Settings app. WindowsI go to Personalization and click Color Settings. Color Change Window Borders Settings Window 11" Width="643" Height="500" > Find the Show accent color on title bar and window borders option, and toggle the switch next to it. To display accent colors on the Start menu and taskbar To display the theme color on the Start menu and taskbar, turn on Show theme on the Start menu and taskbar

The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

Taskbar thumbnails can be fun, but they can also be distracting or annoying. Considering how often you hover over this area, you may have inadvertently closed important windows a few times. Another disadvantage is that it uses more system resources, so if you've been looking for a way to be more resource efficient, we'll show you how to disable it. However, if your hardware specs can handle it and you like the preview, you can enable it. How to enable taskbar thumbnail preview in Windows 11? 1. Using the Settings app tap the key and click Settings. Windows click System and select About. Click Advanced system settings. Navigate to the Advanced tab and select Settings under Performance. Select "Visual Effects"

By default, the title bar color on Windows 11 depends on the dark/light theme you choose. However, you can change it to any color you want. In this guide, we'll discuss step-by-step instructions for three ways to change it and personalize your desktop experience to make it visually appealing. Is it possible to change the title bar color of active and inactive windows? Yes, you can change the title bar color of active windows using the Settings app, or you can change the title bar color of inactive windows using Registry Editor. To learn these steps, go to the next section. How to change title bar color in Windows 11? 1. Using the Settings app press + to open the settings window. WindowsI go to "Personalization" and then

Do you see "A problem occurred" along with the "OOBELANGUAGE" statement on the Windows Installer page? The installation of Windows sometimes stops due to such errors. OOBE means out-of-the-box experience. As the error message indicates, this is an issue related to OOBE language selection. There is nothing to worry about, you can solve this problem with nifty registry editing from the OOBE screen itself. Quick Fix – 1. Click the “Retry” button at the bottom of the OOBE app. This will continue the process without further hiccups. 2. Use the power button to force shut down the system. After the system restarts, OOBE should continue. 3. Disconnect the system from the Internet. Complete all aspects of OOBE in offline mode

We all have different preferences when it comes to display scaling on Windows 11. Some people like big icons, some like small icons. However, we all agree that having the right scaling is important. Poor font scaling or over-scaling of images can be a real productivity killer when working, so you need to know how to customize it to get the most out of your system's capabilities. Advantages of Custom Zoom: This is a useful feature for people who have difficulty reading text on the screen. It helps you see more on the screen at one time. You can create custom extension profiles that apply only to certain monitors and applications. Can help improve the performance of low-end hardware. It gives you more control over what's on your screen. How to use Windows 11

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible

The activation process on Windows sometimes takes a sudden turn to display an error message containing this error code 0xc004f069. Although the activation process is online, some older systems running Windows Server may experience this issue. Go through these initial checks, and if they don't help you activate your system, jump to the main solution to resolve the issue. Workaround – close the error message and activation window. Then restart the computer. Retry the Windows activation process from scratch again. Fix 1 – Activate from Terminal Activate Windows Server Edition system from cmd terminal. Stage – 1 Check Windows Server Version You have to check which type of W you are using
