在PHP中实现分页 [英] Implementing pagination in PHP
本文介绍了在PHP中实现分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在此代码中实现分页:
How to implement a pagination in this code:
<?php
include "connect.php";
$query = mysql_query("SELECT username, date FROM users ORDER BY id ASC");
while($row = mysql_fetch_array($query)):
?>
<div><h5><?php echo $row['username']; ?> (<?php echo $row['date']; ?>)</h5></div>
<?php endwhile; ?>
推荐答案
该想法是使用mysql LIMIT
关键字进行的.它使您可以限制从mysql获得的结果数.
The idea is to play with mysql LIMIT
keyword. It lets you limit the number of results you will get from mysql.
SELECT data FROM table LIMIT 5
等同于
SELECT data FROM table LIMIT 0, 5
其中0是偏移量,5是MySQL返回的行数.
Where 0 is the offset and 5 the number of row returned by MySQL.
因此,您必须确定要显示的项目数量,例如$numRes = 10
和页码:
So you have to fix a number of item to display, let's say $numRes = 10
and a page number:
if (isset($_GET['page']) && is_numeric($_GET['page']))
$page = $_GET['page'];
else
$page = 0;
所以您的要求是这样的:
So you request is something like:
sprintf($request, "SELECT data FROM pages LIMIT %d, %d", $page, $numRes);
这篇关于在PHP中实现分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文