How to implement Pagination?

↘锁芯ラ 提交于 2019-12-25 02:56:03

问题


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']);
}

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?


回答1:


Simplest answer, add LIMIT to your SQL.

LIMIT 10,0 would show first 10 rows.

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');



回答2:


If you're using mysql, you can use the Limit clause. Example:

SELECT * FROM tbl LIMIT 5,10; # Retrieve rows 6-15

found here: http://dev.mysql.com/doc/refman/5.1/en/select.html



来源:https://stackoverflow.com/questions/1619170/how-to-implement-pagination

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!