PHP Tutorial: How to Add Previous and Next Page Buttons to a Web Page
P粉718730956
P粉718730956 2023-08-27 10:22:36
0
1
528
<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粉718730956
P粉718730956

reply all(1)
P粉451614834

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();
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template