[Solved] PHP Code To Display Navigation Links For Records In MySQLi

This code will display a set of navigation links at the bottom of the page, allowing the user to navigate to different pages of records. The number of records displayed per page can be set using the $per_page variable, and the current page number is determined using the $page variable. The navigation links include links to the first and last pages, as well as previous and next links for navigating to adjacent pages.

I hope this helps! Let me know if you have any questions or if you need further assistance.



// Connect to the database
$conn = new mysqli(‘hostname’, ‘username’, ‘password’, ‘database_name’);

// Check for connection errors
if ($conn->connect_error) {
die(“Connection failed: ” . $conn->connect_error);

// Set the number of records to display per page
$per_page = 10;

// Determine the current page number
if (isset($_GET[‘page’]) && is_numeric($_GET[‘page’])) {
$page = $_GET[‘page’];
} else {
$page = 1;

// Calculate the start record
$start_from = ($page-1) * $per_page;

// Select the records for the current page
$sql = “SELECT * FROM table_name LIMIT $start_from, $per_page”;
$result = $conn->query($sql);

// Display the records
while($row = $result->fetch_assoc()) {
echo $row[‘field_name’] . ” ” . $row[‘field_name’] . “<br>”;

// Free the result set

// Determine the total number of pages
$total_pages_sql = “SELECT COUNT(*) FROM table_name”;
$result = $conn->query($total_pages_sql);
$total_rows = $result->fetch_array()[0];
$total_pages = ceil($total_rows / $per_page);

// Close the connection

// Display the navigation links
echo “<div class=’pagination’>”;

if ($page > 1) {
echo “<a href=’?page=1′>First</a>”;
echo “<a href=’?page=”.($page-1).”‘>Prev</a>”;

for ($i=1; $i<=$total_pages; $i++) {
if ($i == $page) {
echo “<strong>$i</strong>”;
} else {
echo “<a href=’?page=$i’>$i</a>”;

if ($page < $total_pages) {
echo “<a href=’?page=”.($page+1).”‘>Next</a>”;
echo “<a href=’?page=$total_pages’>Last</a>”;

echo “</div>”;


Leave a Comment

Your email address will not be published. Required fields are marked *