您好,我正在尝试在页面上显示我的用户数据
这是我的代码:
//Run a query to select all the data from the users table
$perpage = 2;
$result = mysql_query("SELECT * FROM users LIMIT $perpage");
它确实每页仅显示两个,但我想知道如何在底部获取链接到数据的页码
这是我更新的代码
$result = mysql_query("SELECT * FROM users"); // Let's get the query
$nrResults=mysql_num_rows($result); // Count the results
if (($nrResults%$limit)<>0) {
$pmax=floor($nrResults/$limit)+1; // Divide to total result by the number of query
// to display per page($limit) and create a Max page
} else {
$pmax=floor($nrResults/$limit);
}
$result = mysql_query("SELECT * FROM users LIMIT 2 ".(($_GET["page"]-1)*$limit).", $limit");
// generate query considering limit
while($line = mysql_fetch_array( $result ))
{
?>
错误
解析错误:语法错误,E:\xampp\htdocs\Admin.php 第 98 行出现意外的 $end
最佳答案
为了做到这一点,您还需要在 SQL 语句中使用偏移值,因此它是
SELECT * FROM users LIMIT $offset, $perpage
示例:
SELECT * FROM tbl LIMIT 5,10; # Retrieve rows 6-15
然后,要获取要放置在页面底部的链接,您需要获取总数据的计数,将总数除以每页值即可算出您将拥有多少页。
然后根据用户点击的页面设置偏移值。
希望有帮助!
更新:
意外结束很可能意味着您的代码中有一个额外的右括号},这导致页面结束并且后面仍然有更多代码。查看您的代码并匹配括号来修复该问题。您粘贴的代码示例中还存在一些其他问题。
$result = mysql_query("SELECT * FROM users" ); //Note if you have a very large table you probably want to get the count instead of selecting all of the data...
$nrResults = mysql_num_rows( $result );
if( $_GET['page'] ) {
$page = $_GET['page']
} else {
$page = 1;
}
$per_page = 2;
$offset = ($page - 1) * $per_page; //So that page 1 starts at 0, page 2 starts at 2 etc.
$result = mysql_query("SELECT * FROM users LIMIT $offset,$per_page");
while( $line = mysql_fetch_array( $result ) )
{
//Do whatever you want with each row in here
}
希望有帮助
然后您可以使用 nrResults 数字来计算您将有多少页...如果您有 10 条记录并且每页显示 2 条记录,那么您将有 5 页,因此您可以在上打印 5 个链接每个页面的 URL 中都包含正确的页面 #...
关于php - php中的分页显示多个页面的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10317350/