如何在下一页PHP上获取下一个ID

时间:2017-11-06 09:48:45

标签: php mysql

我想通过使用上一个/下一个按钮来阅读不同页面上的不同ID。

如果我点击下一个按钮,我想获得下一个ID,如果我点击上一个按钮,它将返回到之前的ID。以下是我想要的。

示例:

  1. testnext_pre1.php? id = 1 & page = 1
  2. testnext_pre1.php的 ID = 2 &安培;页= 2
  3. testnext_pre1.php的 ID = 5 &安培;页= 3
  4. 这是我的问题。如您所见,由于我的代码,我在每个页面上都获得相同的ID。如何获取页面的正确ID?

    enter image description here enter image description here 请注意: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;
    

4 个答案:

答案 0 :(得分:1)

尝试以下sql查询来获取下一个id,现在你正在循环新闻表,你的下一个按钮将始终具有相同的值

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

        $resultid = mysql_query("select id from news where id = (select min(id) from news where id > ".$_GET['id'].")");
        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> ";
        }
$prevresultid = mysql_query("select id from news where id = (select max(id) from news where id < ".$_GET['id'].")");
        while($loopid=mysql_fetch_array($prevresultid))
        { 
            $rowid = $loopid['id'];
            $prev= "  <div class=\"paginationbtn floatleft\"><a href=\"$phpself?id=$rowid&amp;page=$page\" title=\"Page $page\">next</a></div> ";
        }
    }

注意:您需要mysqli和预备语句来保护您的代码

答案 1 :(得分:1)

从数据库中获取上一个和下一个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
}

答案 2 :(得分:0)

如果你想设置分页而不需要下一个id,它只会被查询管理。在查询中我们只改变偏移量。 例如,每页显示3条记录,因此我们的限制为3 现在,如果我们在第一页上比我们的获取记录查询是(在第一页偏移总是0) 所以offset是0,limit是3 SELECT fieldName FROM tableName LIMIT offset,limit

对于第二页现在当前偏移是3所以

prev是current_offset减去限制(3-3)所以prev offset是0 next是current_offset加上限制(3 + 3)所以下一个偏移是6

现在在第3页当前偏移是6

prev是current_offset减去限制(6-3)所以prev offset是3 next是current_offset加上限制(6 + 3)所以下一个偏移是9

答案 3 :(得分:0)

尝试这个

<?php
$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";//offset,limit
$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)//current page > 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;
    $next_row=$previousRows+1;
    $resultid = mysql_query("SELECT id FROM news LIMIT $next_row,1");
    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;