Rumah >pembangunan bahagian belakang >tutorial php >快速掌握PHP文章分页技巧_PHP教程
1、前言
对于web编程的老手来说,编写这种代码实在是和呼吸一样自然,但是对于初学者来说,常常对这个问题摸不着头绪,因此特地撰写此文对这个问题进行详细的讲解,力求让看完这篇文章的朋友在看完以后对于分页显示的原理和实现方法有所了解。本文适合初学者阅读,所有示例代码均使用php编写。
2、PHP文章分页原理
所谓分页显示,也就是将数据库中的结果集人为的分成一段一段的来显示,这里需要两个初始的参数:
每页多少条记录($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文章分页简单代码
请详细阅读以下代码,自己调试运行一次,最好把它修改一次,加上自己的功能,比如搜索等等。
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><span> ?php </span></span></span></li> <li><span>// 建立数据库连接 </span></li> <li class="alt"> <span>$</span><span class="attribute">link</span><span> = </span><span class="attribute-value">mysql_connect</span><span>("localhost", <br>"mysql_user", "mysql_password") </span> </li> <li><span>or die("Could not connect: " . mysql_error()); </span></li> <li class="alt"><span>// 获取当前页数 </span></li> <li><span>if( isset($_GET['page']) ){ </span></li> <li class="alt"> <span>$</span><span class="attribute">page</span><span> = </span><span class="attribute-value">intval</span><span>( $_GET['page'] ); </span> </li> <li><span>} </span></li> <li class="alt"><span>else{ </span></li> <li> <span>$</span><span class="attribute">page</span><span> = </span><span class="attribute-value">1</span><span>; </span> </li> <li class="alt"><span>} </span></li> <li><span>// 每页数量 </span></li> <li class="alt"> <span>$</span><span class="attribute">PageSize</span><span> = </span><span class="attribute-value">10</span><span>; </span> </li> <li><span>// 获取总数据量 </span></li> <li class="alt"> <span>$</span><span class="attribute">sql</span><span> = </span><span class="attribute-value">"select count(*) as amount from table"</span><span>; </span> </li> <li> <span>$</span><span class="attribute">result</span><span> = </span><span class="attribute-value">mysql_query</span><span>($sql); </span> </li> <li class="alt"> <span>$</span><span class="attribute">row</span><span> = </span><span class="attribute-value">mysql_fetch_row</span><span>($result); </span> </li> <li> <span>$</span><span class="attribute">amount</span><span> = $row['amount']; </span> </li> <li class="alt"><span>// 记算总共有多少页 </span></li> <li><span>if( $amount ){ </span></li> <li class="alt"> <span>if( $amount </span><span class="tag"><span> $page_size ){ $</span><span class="attribute">page_count</span><span> = </span><span class="attribute-value">1</span><span>; } </span></span> </li> <li class="alt"><span>//如果总数据量小于$PageSize,那么只有一页 </span></li> <li><span>if( $amount % $page_size ){ </span></li> <li><span>//取总数据量除以每页数的余数 </span></li> <li class="alt"> <span>$</span><span class="attribute">page_count</span><span> = (int)($amount / $page_size) + 1; </span> </li> <li class="alt"><span>//如果有余数,则页数等于总数据量除以每页数的结<br>果取整再加一 </span></li> <li><span>}else{ </span></li> <li class="alt"> <span>$</span><span class="attribute">page_count</span><span> = $amount / $page_size; </span> </li> <li class="alt"><span>//如果没有余数,则页数等于总数据量除以每页数的结果 </span></li> <li><span>} </span></li> <li class="alt"><span>} </span></li> <li><span>else{ </span></li> <li class="alt"> <span>$</span><span class="attribute">page_count</span><span> = </span><span class="attribute-value">0</span><span>; </span> </li> <li><span>} </span></li> <li class="alt"><span> </span></li> <li><span>// 翻页链接 </span></li> <li class="alt"> <span>$</span><span class="attribute">page_string</span><span> = </span><span class="attribute-value">''</span><span>; </span> </li> <li> <span>if( $</span><span class="attribute">page</span><span> == 1 ){ </span> </li> <li class="alt"> <span>$page_string </span><span class="attribute">.</span><span>= </span><span class="attribute-value">'第一页|上一页|'</span><span>; </span> </li> <li><span>} </span></li> <li class="alt"><span>else{ </span></li> <li> <span>$page_string </span><span class="attribute">.</span><span>= </span><span class="attribute-value">'<a>第一页</a><br>|<a><span>.($page-1).'</span><span class="tag">></span><span>上一页</span><span class="tag"></span><span class="tag-name">a</span><span class="tag">></span><span>|'; </span></a></span> </li> <li class="alt"><span>} </span></li> <li> <span>if( ($</span><span class="attribute">page</span><span> == $page_count) || ($</span><span class="attribute">page_count</span><span> == 0) ){ </span> </li> <li class="alt"> <span>$page_string </span><span class="attribute">.</span><span>= </span><span class="attribute-value">'下一页|尾页'</span><span>; </span> </li> <li><span>} </span></li> <li class="alt"><span>else{ </span></li> <li> <span>$page_string </span><span class="attribute">.</span><span>= </span><span class="attribute-value">'<a><span>.($page+1).'</span><span class="tag">><br></span><span>下一页</span><span class="tag"></span><span class="tag-name">a</span><span class="tag">></span><span>|</span><span class="tag"><span class="tag-name">a</span><span> </span><span class="attribute">href</span><span>=?</span><span class="attribute">page</span><span>=</span><span class="attribute-value">'.$page_count.'</span><span class="tag">></span><span>尾页</span><span class="tag"></span><span class="tag-name">a</span><span class="tag">></span><span>'; </span></span></a></span> </li> <li class="alt"><span>} </span></li> <li><span>// 获取数据,以二维数组格式返回结果 </span></li> <li class="alt"><span>if( $amount ){ </span></li> <li> <span>$</span><span class="attribute">sql</span><span> = </span><span class="attribute-value">"select * from table order by id desc <br>limit "</span><span>. ($page-1)*$page_size .", $page_size"; </span> </li> <li class="alt"> <span>$</span><span class="attribute">result</span><span> = </span><span class="attribute-value">mysql_query</span><span>($sql); </span> </li> <li><span> </span></li> <li class="alt"> <span>while ( $</span><span class="attribute">row</span><span> = </span><span class="attribute-value">mysql_fetch_row</span><span>($result) ){ </span> </li> <li><span>$rowset[] = $row; </span></li> <li class="alt"><span>} </span></li> <li><span>}else{ </span></li> <li class="alt"> <span>$</span><span class="attribute">rowset</span><span> = </span><span class="attribute-value">array</span><span>(); </span> </li> <li><span>} </span></li> <li class="alt"><span>// 没有包含显示结果的代码,那不在讨论范围,<br>只要用foreach就可以很简单的用得到的二维数组来显示结果 </span></li> <li> <span class="tag">?></span><span> </span> </li> </ol>
以上就是PHP文章分页的相关实现方法。