我以前使用过此代码没有任何问题,但是...现在我无法按下上一页/下一页导航时加载页面请求,如果你能看到我出错的地方是一个帮助。
<?php
include 'config.php';
$db = mysql_connect($host, $username, $password);
mysql_select_db($db_name,$db);
$sql = "SELECT u_id FROM users";
$query = mysql_query($sql,$db);
$total_results = mysql_num_rows($query);
$limit = "5"; //limit of archived results per page.
$total_pages = ceil($total_results / $limit); //total number of pages
if (empty($page))
{
$page = "1"; //default page if none is selected
}
$offset = ($page - 1) * $limit; //starting number for displaying results out of DB
$query = "SELECT * FROM users ORDER BY u_id LIMIT $offset, $limit";
$result = mysql_query($query);
//This is the start of the normal results...
while ($row = mysql_fetch_array($result))
{
?>
<HR>
Username : <B><?echo $row['uname'] ?></B><BR>
user Message: <?echo $row['umess'] ?>
<BR>
<?
}
mysql_close();
// This is the Previous/Next Navigation stuff having issue with
echo "<font face=Verdana size=1>";
echo "Pages:($total_pages) "; // total pages
if ($page != 1)
{
echo "<a href=?&page=1><< First</a> "; // First Page Link
$prevpage = $page - 1;
echo " <a href=?page=$prevpage><<</a> "; // Previous Page Link
}
if ($page == $total_pages)
{
$to = $total_pages;
}
elseif ($page == $total_pages-1)
{
$to = $page+1;
}
elseif ($page == $total_pages-2)
{
$to = $page+2;
}
else
{
$to = $page+3;
}
if ($page == 1 || $page == 2 || $page == 3)
{
$from = 1;
}
else
{
$from = $page-3;
}
for ($i = $from; $i <= $to; $i++)
{
if ($i == $total_results) $to=$total_results;
if ($i != $page)
{
echo "<a href=?showold=yes&page=$i>$i</a>";
}
else
{
echo "<b><font face=Verdana size=2>[$i]</font></b>";
}
if ($i != $total_pages)
echo " ";
}
if ($page != $total_pages)
{
$nextpage = $page + 1;
echo " <a href=?page=$nextpage>>></a> "; // Next Page Link
echo " <a href=?page=$total_pages>Last >></a>"; // Last Page Link
}
echo "</font>";
?>
在测试运行之后,您可以看到第一页请求很好,直到您点击下一页或页码继续回到第一页。
答案 0 :(得分:1)
而不是
if (empty($page))
{
$page = "1"; //default page if none is selected
}
尝试:
if (isset($_GET['page']))
{
$page = $_GET['page'];
}
else
{
$page = 1;
}