1

我想使用 mysql_fetch_array 显示数据库中的记录。但问题是我不知道如何修复页面中显示的大小并将其余记录移动到下一页,因为要显示的记录太多。

例如:在 page1 中显示前 10 行(或记录),然后在另一页中显示另外 10 行?

这是我的代码:

$result=mysql_query("$selectKL UNION $selectKlang UNION $selectPJ UNION $selectSJ 
ORDER BY restaurant_name LIMIT 6")

$searchQuery = search($budgetRange,$city,$category);

while($row = mysql_fetch_array($searchQuery))
{
   echo"<tr>";
   echo'<td>'.$row['restaurant_id'].'</b></td>';
   echo'<td>'.$row['restaurant_name'].'</b></td>';
   echo'<td>'.$row['category'].'</b></td>';
   echo'<td>'.$row['budget'].'</b></td>';
   echo'<td>'.$row['halal'].'</b></td>';
   echo "</tr>";
}

我的数据库中将显示 30 多个结果,但我如何在第一页显示前 10 个结果,然后在下一页显示另外 10 个结果,依此类推?我已经在查询中将 LIMIT 设置为 10,是的,它只显示 10 个结果,但我不知道如何存储或传递其余结果(可能吗?)。任何帮助表示赞赏...拜托?

4

2 回答 2

1

分页可能很棘手。您需要设置一些参数(可能是查询字符串参数)以指示足够的信息来知道在哪里获取分页,即每页的结果数和页码,或者显示的最后一个结果的数量.

在您的查询中,您可以使用LIMIT x,y其中 x 是起始行号,y 是要检索的行数。

分页是一个已经解决了很多次的问题——这并不意味着它很容易,但它意味着有很多可用的帮助。尝试快速搜索:https ://encrypted.google.com/search?hl=en&q=php+mysql+pagination

于 2013-03-18T18:28:00.417 回答
0
/*
    Place code to connect to your DB here.
*/


$tbl_name="xyz";        //your table name
// How many adjacent pages should be shown on each side?
$adjacents = 3;

/* 
   First get total number of rows in data table. 
   If you have a WHERE clause in your query, make sure you mirror it here.
*/
$query = "SELECT COUNT(*) as num FROM $tbl_name WHERE approved`=1  ";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages['num'];

/* Setup vars for query. */
$targetpage = "index.php";  //your file name  (the name of this file)
$limit = 5;                                 //how many items to show per page
$page = (!empty($_GET['page']) ? $_GET['page'] : null);

if($page) 
    $start = ($page - 1) * $limit;          //first item to display on this page
else
    $start = 0;                             //if no page var is given, set start to 0

/* Get data. */
$sql = "SELECT * FROM $tbl_name WHERE `approved`=1  ORDER BY `id` DESC LIMIT $start, $limit ";
$result = mysql_query($sql);

/* Setup page vars for display. */
if ($page == 0) $page = 1;                  //if no page var is given, default to 1.
$prev = $page - 1;                          //previous page is page - 1
$next = $page + 1;                          //next page is page + 1
$lastpage = ceil($total_pages/$limit);      //lastpage is = total pages / items per page, rounded up.
$lpm1 = $lastpage - 1;                      //last page minus 1

include ('includes/pagination.php');


    while($row = mysql_fetch_array($result))
    {

    //designate variables




echo  'Your content';
}
$pagination
于 2013-03-18T18:47:46.290 回答