How can I display a long list of database entries on multiple pages, showing only 30 entries per page and providing a "NEXT" link for the next set?
To display a long list of database entries on multiple pages, you can use pagination. This involves limiting the number of entries shown per page (e.g., 30 entries) and providing navigation links to move between pages. You can achieve this by using SQL's LIMIT and OFFSET clauses to fetch a specific subset of entries for each page.
<?php
// Set the number of entries per page
$entries_per_page = 30;
// Get the current page number
$current_page = isset($_GET['page']) ? $_GET['page'] : 1;
// Calculate the OFFSET for the SQL query
$offset = ($current_page - 1) * $entries_per_page;
// Query the database to fetch entries for the current page
$query = "SELECT * FROM your_table LIMIT $entries_per_page OFFSET $offset";
// Execute the query and display the entries
// Display navigation links for previous and next pages
if ($current_page > 1) {
echo '<a href="?page='.($current_page - 1).'">Previous</a>';
}
// Check if there are more entries for the next page
$next_page_query = "SELECT COUNT(*) FROM your_table LIMIT $entries_per_page OFFSET ".($offset + $entries_per_page);
$next_page_result = mysqli_query($connection, $next_page_query);
$next_page_count = mysqli_fetch_row($next_page_result)[0];
if ($next_page_count > 0) {
echo '<a href="?page='.($current_page + 1).'">Next</a>';
}
?>
Keywords
Related Questions
- In PHP, what are some best practices for setting values, such as email recipients, within a class using functions?
- Are there any performance considerations to keep in mind when manipulating strings in PHP?
- What are the potential issues with using the same ID multiple times in an HTML document when using DOMDocument in PHP?