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.
1 Answer
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();
5 Comments
ziico
hmm ok. but how should I hide my Next Label/button if it reach my max record?
Omar Tammam
you can max a calculation to get that
$page*$recordsPerPage >= $totalziico
I'm kinda confused, can you please show me the whole code pls?
ziico
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.
Omar Tammam
@ziico example added to the answer kindly check , and please update the DB variables with yours
$page? And why it isn't$id?idis 1, your limit will be0: from(($id - 1) * $recordsPerPage)because(1 - 1)times anything, is0.