I'm attempting to write a script that returns rows from a mySQL database. Basically, the users on my site receive "experience points" and go up "ranks" based on the number of experience points they get.
我正在尝试编写一个从mySQL数据库返回行的脚本。基本上,我网站上的用户会收到“经验值”,并根据他们获得的经验值数量上升“排名”。
I have a page called "recentexp.php" where all recent exp is shown in a table, with the most recent at the top.
我有一个名为“recentexp.php”的页面,其中所有最近的exp都显示在一个表格中,最近的一个在顶部。
How do I write a code that creates more pages the more results are returned?
如何编写创建更多页面的代码,返回的结果越多?
If there are less than 10 rows, then have no page numbers, and only show page 1. However, if there are 12 rows returned, then have the 10 most recent (with the MOST recent at the top) on my page, and then have PHP create a page "2" where the 2 oldest rows are placed.
如果少于10行,则没有页码,只显示第1页。但是,如果有12行返回,则在我的页面上有最近的10行(最近的MOST),然后让PHP创建一个页面“2”,其中放置了最旧的2行。
If I explained this badly, I apologize. I'm exhausted.. long day.
如果我解释得很严重,我道歉。我筋疲力尽......漫长的一天。
Thanks!
谢谢!
1 个解决方案
#1
11
You can use LIMIT x, y
to show the y
results after x
.
您可以使用LIMIT x,y来显示x后的y结果。
Let's say you have a variable $page
that is passed through $_GET
or through some other means. $page
defaults to 1, or is a numerical value that denotes the page number.
假设您有一个通过$ _GET或其他方式传递的变量$ page。 $ page默认为1,或者是表示页码的数值。
You can then select the rows of that page via:
然后,您可以通过以下方式选择该页面的行:
SELECT * FROM `table` WHERE `condition` = ? LIMIT ($page - 1) * 10, 10
You can replace 10 with how many results per page. As you can see, if the page number is 1, this query is:
您可以将每页的结果数替换为10。如您所见,如果页码为1,则此查询为:
SELECT * FROM `table` WHERE `condition` = ? LIMIT 0, 10
Which displays the first 10 results. If the page number is 2, this query is:
显示前10个结果。如果页码为2,则此查询为:
SELECT * FROM `table` WHERE `condition` = ? LIMIT 10, 10
Which displays the ten rows after the 10th row, i.e., rows 11-20 (which should be on page 2!)
显示第10行之后的10行,即第11-20行(应该在第2页!)
#1
11
You can use LIMIT x, y
to show the y
results after x
.
您可以使用LIMIT x,y来显示x后的y结果。
Let's say you have a variable $page
that is passed through $_GET
or through some other means. $page
defaults to 1, or is a numerical value that denotes the page number.
假设您有一个通过$ _GET或其他方式传递的变量$ page。 $ page默认为1,或者是表示页码的数值。
You can then select the rows of that page via:
然后,您可以通过以下方式选择该页面的行:
SELECT * FROM `table` WHERE `condition` = ? LIMIT ($page - 1) * 10, 10
You can replace 10 with how many results per page. As you can see, if the page number is 1, this query is:
您可以将每页的结果数替换为10。如您所见,如果页码为1,则此查询为:
SELECT * FROM `table` WHERE `condition` = ? LIMIT 0, 10
Which displays the first 10 results. If the page number is 2, this query is:
显示前10个结果。如果页码为2,则此查询为:
SELECT * FROM `table` WHERE `condition` = ? LIMIT 10, 10
Which displays the ten rows after the 10th row, i.e., rows 11-20 (which should be on page 2!)
显示第10行之后的10行,即第11-20行(应该在第2页!)