Home > Mobile >  how to make previous and next button in php
how to make previous and next button in php

Time:04-09

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.

CodePudding user response:

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();
  • Related