如何实现分页? [英] How to implement Pagination?

查看:116
本文介绍了如何实现分页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实现简单分页的最佳方法是什么? 这是im用来将数据库中的项目放入表中的代码:

What is the best way to implement simple pagination? Here is the code im using to put the items from the database into a table:

$sql = "SELECT * FROM table WHERE id='id' ";
$result = mysql_query($sql);
while($row=mysql_fetch_array($result))}
     echo($row['id']);
     echo($row['name']);
}

我只是想对此进行分页,所以我将使用$ _GET ['page'](bla.php?page = 1)将偏移量设置为10,然后(bla.php?page = 2)将其设置为20?

I just wanted to pageinate this so i would use $_GET['page'] (bla.php?page=1) to set the offset to 10, then (bla.php?page=2) to set it by 20?

推荐答案

最简单的答案,在您的SQL中添加LIMIT.

Simplest answer, add LIMIT to your SQL.

LIMIT 10,0将显示前10行.

LIMIT 10,10将显示10行,从第10行开始.

LIMIT 10,10 would show 10 rows, starting at row 10.

请注意,将其放入查询中时,需要对其进行清理. 对于应该是整数的用户提供的输入,请通过静默更改用户输入的类型来确保输入是

As a side note, when putting this in your queries you need to sanitize it. For user provided input that is supposed to be an integer, ensure it is by silently changing the the user input type.

$limit = $_GET['limit'];
settype($limit, 'integer');

这篇关于如何实现分页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆