0

我之前使用过这段代码没有任何问题,但是......现在我似乎无法在按下上一个/下一个导航时加载页面请求,如果你能看到我出错的地方是一个帮助。

<?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)&nbsp;&nbsp;"; // total pages
if ($page != 1)
{
echo "<a href=?&page=1><< First</a>&nbsp;&nbsp;&nbsp;"; // First Page Link
$prevpage = $page - 1;
echo "&nbsp;<a href=?page=$prevpage><<</a>&nbsp;"; // 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 "&nbsp;";
    }
if ($page != $total_pages)
{
$nextpage = $page + 1;
echo "&nbsp;<a href=?page=$nextpage>>></a>&nbsp;"; // Next Page Link
echo "&nbsp;&nbsp;&nbsp;<a href=?page=$total_pages>Last >></a>"; // Last Page Link
}
echo "</font>";



?>

测试运行后,您可以看到第一页请求很好,直到您单击下一步或页码它会一直返回到第一页。

4

1 回答 1

1

代替

if (empty($page))
{
    $page = "1"; //default page if none is selected
}

尝试 :

if (isset($_GET['page']))
{
$page = $_GET['page'];
}
else
{
$page = 1;
}
于 2012-08-26T01:45:53.467 回答