July 9, 2025

Here is an example of a PHP program for pagination with multiple rows and columns:

<?php
// Establish a database connection
$db = new mysqli('localhost', 'username', 'password', 'database_name');

// Set the number of results to display per page
$results_per_page = 10;

// Retrieve the total number of results from the database
$sql = "SELECT COUNT(*) FROM table_name";
$result = $db->query($sql);
$row = $result->fetch_row();
$total_results = $row[0];

// Calculate the total number of pages
$total_pages = ceil($total_results / $results_per_page);

// Determine the current page
if (!isset($_GET['page'])) {
    $current_page = 1;
} else {
    $current_page = $_GET['page'];
}

// Calculate the starting and ending result numbers for the current page
$start_index = ($current_page - 1) * $results_per_page;
$end_index = $start_index + $results_per_page - 1;

// Retrieve the results from the database for the current page
$sql = "SELECT * FROM table_name LIMIT $start_index, $results_per_page";
$result = $db->query($sql);

// Display the results in a table
echo '<table>';
while ($row = $result->fetch_assoc()) {
    echo '<tr>';
    echo '<td>' . $row['column1'] . '</td>';
    echo '<td>' . $row['column2'] . '</td>';
    echo '<td>' . $row['column3'] . '</td>';
    // add more columns as needed
    echo '</tr>';
}
echo '</table>';

// Display the pagination links
echo '<div class="pagination">';
for ($i = 1; $i <= $total_pages; $i++) {
    if ($i == $current_page) {
        echo '<span class="current">' . $i . '</span>';
    } else {
        echo '<a href="?page=' . $i . '">' . $i . '</a>';
    }
}
echo '</div>';

// Close the database connection
$db->close();
?>

In this example, the program retrieves the total number of results from a database table and calculates the total number of pages based on the number of results to display per page. It then determines the current page based on the page parameter in the URL, and calculates the starting and ending result numbers for the current page. The program retrieves the results from the database for the current page and displays them in a table. Finally, the program displays pagination links for each page, with the current page highlighted.

About The Author

Leave a Reply

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