search

Home  >  Q&A  >  body text

PHP Tutorial: How to Add Previous and Next Page Buttons to a Web Page

<p>I'm getting my songs from a MySQL database and will be adding more songs in the future, so there's no way to hardcode a maximum number. I would be very grateful for any help or advice. </p>
P粉718730956P粉718730956454 days ago526

reply all(1)I'll reply

  • P粉451614834

    P粉4516148342023-08-28 00:31:38

    You can use the following query to get the total number of records in the database

    $query = 'SELECT count(*) as total FROM song';

    Used to check whether the maximum value is reached

    <? if ($page*$recordsPerPage < $total) : ?>
    <a href="page/details.php?id=<?= $id + 1 ?>">下一页</a>

    Full example of hiding next page

    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "test2";
    
    // 创建连接
    $conn = new mysqli($servername, $username, $password, $dbname);
    // 检查连接
    if ($conn->connect_error) {
      die("连接失败: " . $conn->connect_error);
    }
    if (!isset($_GET['id'])) {
            $id = 1;
         } else {
            $id = (int)$_GET['id'];
         }
         
         $recordsPerPage = 10 ;
         
       //  $query = 'SELECT * FROM song WHERE 1 LIMIT ' . (($id - 1) * $recordsPerPage) . ' ' . $recordsPerPage;
       // 这里需要处理歌曲数据的获取   
             
             
    $sql = "select count(*) as total FROM songs";
    $result = $conn->query($sql);
    $data = $result->fetch_assoc();
    
    
    if ($id*$recordsPerPage < (int)$data["total"])
    echo "<a href='page/details.php?id=". ($id+1) ."'>下一页</a>";
    
    $conn->close();

    reply
    0
  • Cancelreply