search
Homephp教程php手册所谓PHP分页也就这么回事

所谓PHP分页也就这么回事

Jun 13, 2016 am 09:42 AM
phpPaginationdatabaseshowofresult

所谓分页显示,也就是将数据库中的结果集人为的分成一段一段的来显示,这里需要两个初始的参数:
每页多少条记录($PageSize)?
当前是第几页($CurrentPageID)?
现在只要再给我一个结果集,我就可以显示某段特定的结果出来。
至于其他的参数,比如:上一页($PReviousPageID)、下一页($NextPageID)、总页数($numPages)等等,都可以根据前边这几个东西得到。
以MySQL数据库为例,如果要从表内截取某段内容,sql语句可以用:select * from table limit offset, rows。看看下面一组sql语句,尝试一下发现其中的规率。
前10条记录:select * from table limit 0,10
第11至20条记录:select * from table limit 10,10
第21至30条记录:select * from table limit 20,10
……
这一组sql语句其实就是当$PageSize=10的时候取表内每一页数据的sql语句,我们可以总结出这样一个模板:
select * from table limit ($CurrentPageID - 1) * $PageSize, $PageSize
拿这个模板代入对应的值和上边那一组sql语句对照一下看看是不是那么回事。搞定了最重要的如何获取数据的问题以后,剩下的就仅仅是传递参数,构造合适的sql语句然后使用php从数据库内获取数据并显示了。以下我将用具体代码加以说明。
3、简单代码
请详细阅读以下代码,自己调试运行一次,最好把它修改一次,加上自己的功能,比如搜索等等。
[php] 
// 建立数据库连接 
$link = mysql_connect("localhost", "mysql_user", "mysql_passWord")  
      or die("Could not connect: " . mysql_error());  
// 获取当前页数 
if( isset($_GET['page']) ){ 
   $page = intval( $_GET['page'] ); 

else{ 
   $page = 1; 
}  
// 每页数量 
$PageSize = 10;  
// 获取总数据量 
$sql = "select count(*) as amount from table"; 
$result = mysql_query($sql); 
$row = mysql_fetch_row($result); 
$amount = $row['amount'];  
// 记算总共有多少页 
if( $amount ){ 
   if( $amount    if( $amount % $page_size ){                                  //取总数据量除以每页数的余数 
       $page_count = (int)($amount / $page_size) + 1;           //如果有余数,则页数等于总数据量除以每页数的结果取整再加一 
   }else{ 
       $page_count = $amount / $page_size;                      //如果没有余数,则页数等于总数据量除以每页数的结果 
   } 

else{ 
   $page_count = 0; 

// 翻页链接 
$page_string = ''; 
if( $page == 1 ){ 
   $page_string .= '第一页|上一页|'; 

else{ 
   $page_string .= '第一页|上一页|'; 
}  
if( ($page == $page_count) || ($page_count == 0) ){ 
   $page_string .= '下一页|尾页'; 

else{ 
   $page_string .= '下一页|尾页'; 

// 获取数据,以二维数组格式返回结果 
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(); 

// 没有包含显示结果的代码,那不在讨论范围,只要用foreach就可以很简单的用得到的二维数组来显示结果 
?> 

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),