-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.

4
  • I think this answer will help you stackoverflow.com/questions/47133998/… Commented Apr 8, 2022 at 22:26
  • Does this answer your question? Count table rows Commented Apr 8, 2022 at 23:04
  • What's $page? And why it isn't $id? Commented Apr 8, 2022 at 23:11
  • Whenever id is 1, your limit will be 0: from (($id - 1) * $recordsPerPage) because (1 - 1) times anything, is 0. Commented Apr 8, 2022 at 23:38

1 Answer 1

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();
Sign up to request clarification or add additional context in comments.

5 Comments

hmm ok. but how should I hide my Next Label/button if it reach my max record?
you can max a calculation to get that $page*$recordsPerPage >= $total
I'm kinda confused, can you please show me the whole code pls?
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 example added to the answer kindly check , and please update the DB variables with yours

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.