我想使用prev / next按钮在不同页面上读取不同的ID。

如果单击下一步按钮,则要获取下一个ID;如果单击上一个按钮,它将返回到先前的ID。以下是我想要的。

例:


testnext_pre1.php?id = 1&page = 1
testnext_pre1.php?id = 2&page = 2
testnext_pre1.php?id = 5&page = 3


这是我的问题。如您所见,由于我的代码,我在每个页面上都获得了相同的ID。如何获取页面的正确ID?

php - 如何获取下一页PHP的下一个ID-LMLPHP
php - 如何获取下一页PHP的下一个ID-LMLPHP
请注意:ID不会按顺序增加,因为某些内容可能会删除。所以我不想要像“ +1”之类的答案。

$rowsPerPage = 1;

if(isset($_GET['page']))
{
    $pageNum= $_GET['page'];
}
else
{
    $pageNum = 1;
}

// preceding rows
$previousRows =($pageNum - 1) * $rowsPerPage;

$query = "SELECT * FROM news LIMIT $previousRows, $rowsPerPage";
$result = mysql_query($query) or die('Error couldn\'t get the data').mysql_error();

    echo "<table border=1>\n";
    echo "<tr><th>ID</th><th>Name</th><th>Password</th><th>Perm</th><th>Email</th>
    <th>Date</th></tr>";
    // print the results
    while(list($id,$name,$pass,$perm,$email,$date) = mysql_fetch_array($result))
    {
        echo "<tr><td>$id</td><td>$name</td><td>$pass</td><td>$perm</td><td>$email</td>
        <td>$date</td></tr>";
    }
    echo '</table>';


$query = "SELECT COUNT(id) AS numrows FROM news";
$result = mysql_query($query) or die('Error, couldn\'t get count title=\"$page\"').mysql_error();

$row = mysql_fetch_assoc($result);
$numrows = $row['numrows'];

$lastPage = ceil($numrows/$rowsPerPage);
$phpself = $_SERVER['PHP_SELF'];


if ($pageNum > 1)
{
    $page = $pageNum - 1;
    $prev = "<div class=\"paginationbtn floatleft\"><a href=\"$phpself?id=$rowid&amp;page=$page\">previous</a></div>";
    $first = " <a href=\"$phpself?page=1\" title=\"Page 1\">[First Page]</a> ";
}
else
{
    $prev = ' previous ';
    $first = ' [First Page] ';
}

if ($pageNum < $lastPage)
{
    $page = $pageNum + 1;

    $resultid = mysql_query("SELECT id FROM news");
    while($loopid=mysql_fetch_array($resultid))
    {
        $rowid = $loopid['id'];
        $next = "  <div class=\"paginationbtn floatright\"><a href=\"$phpself?id=$rowid&amp;page=$page\" title=\"Page $page\">next</a></div> ";
    }
}

else
{
    $next = ' [Next] ';
}

echo $prev . " " . $next;

最佳答案

只需从数据库中获取上一个和下一个ID

if ($pageNum > 1)
{
   //Get previous id using this query
   SELECT id FROM news LIMIT $previousRows-1, $rowsPerPage
}



if ($pageNum < $lastPage)
{
   //Get next id using this query
   SELECT id FROM news LIMIT $previousRows+1, $rowsPerPage
}

09-07 23:10
查看更多