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

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

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),

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)