如何在 php 中制作上一个和下一个按钮

how to make previous and next button in php

我正在从 MySQL 数据库中获取我的歌曲,我会在未来添加更多歌曲,所以我不能硬编码最大数量所以如果我能得到我将不胜感激任何帮助或建议如何去做。

您可以使用以下查询来获取数据库中的总记录数

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

用于检查是否达到最大值

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

隐藏下一个的完整示例

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test2";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $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;
   //Here need to handle the songs data retreive   
         
         
$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) ."'>Next</a>";

$conn->close();