我想使用mysql_fetch_array显示数据库中的记录。但问题是我不知道如何修改要在页面中显示的大小并将其余记录移动到下一页,因为要显示的记录太多。
例如:在page1中显示前10行(或记录),然后在另一页中显示另外10行?
Heres是我的代码:
$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个结果,但我不知道如何存储或传递结果的其余部分(是否可能?)。任何帮助都很感激......拜托?
答案 0 :(得分:1)
分页可能很棘手。您需要设置一些参数(可能是查询字符串参数)来指示足够的信息以了解在哪里获取您的分页,即每页的结果数和页码,或者显示的最后结果的数量
在您的查询中,您可以使用LIMIT x,y
,其中x是起始行号,y是要检索的行数。
分页是一个已经解决过多次的问题 - 这并不意味着它很容易,但这意味着有很多可用的帮助。尝试快速搜索:https://encrypted.google.com/search?hl=en&q=php+mysql+pagination
答案 1 :(得分: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