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.
Asked
Active
Viewed 593 times
-1
-
I think this answer will help you https://stackoverflow.com/questions/47133998/how-to-get-next-id-on-next-page-php – Ahmed Hassan Apr 08 '22 at 22:26
-
Does this answer your question? [Count table rows](https://stackoverflow.com/questions/1893424/count-table-rows) – gre_gor Apr 08 '22 at 23:04
-
What's `$page`? And why it isn't `$id`? – gre_gor Apr 08 '22 at 23:11
-
Whenever `id` is 1, your limit will be `0`: from `(($id - 1) * $recordsPerPage)` because `(1 - 1)` times anything, is `0`. – mardubbles Apr 08 '22 at 23:38
1 Answers
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 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