-1

I'm fetching my songs from MySQL database and I'll be adding more song in the futures so I can't hard coded the max number so I'll be really appreciated if I can get any help or suggestion how to do it.

Jonas
  • 121,568
  • 97
  • 310
  • 388
ziico
  • 449
  • 8
  • 23

1 Answers1

1

you can use the following query to get the total records in DB

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

for checking if the max is reached

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

complete example for hiding the next

$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();
Omar Tammam
  • 1,217
  • 1
  • 9
  • 17
  • hmm ok. but how should I hide my Next Label/button if it reach my max record? – ziico Apr 08 '22 at 22:36
  • you can max a calculation to get that `$page*$recordsPerPage >= $total` – Omar Tammam Apr 08 '22 at 22:39
  • I'm kinda confused, can you please show me the whole code pls? – ziico Apr 08 '22 at 22:41
  • I'm still seeing next label/button even if I reach my max id song tho. I have 33 record in my db and next button is still appearing even at 33. If a click on it then it still counting 34,35,36...++ even though I don't have those Id in my db. – ziico Apr 08 '22 at 22:46
  • @ziico example added to the answer kindly check , and please update the DB variables with yours – Omar Tammam Apr 08 '22 at 22:56