How can pagination be implemented in PHP to display search results in batches of 10?
To implement pagination in PHP to display search results in batches of 10, you can use the LIMIT clause in your SQL query to fetch only a specific number of rows at a time. You can also use GET parameters to keep track of the current page number and calculate the offset for each page.
<?php
// Assuming you have already connected to your database
// Get the current page number from the URL
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$offset = ($page - 1) * 10;
// Query to fetch search results with pagination
$query = "SELECT * FROM your_table LIMIT $offset, 10";
$result = mysqli_query($conn, $query);
// Display search results
while ($row = mysqli_fetch_assoc($result)) {
// Display search result data
}
// Pagination links
$total_results = mysqli_num_rows(mysqli_query($conn, "SELECT * FROM your_table"));
$total_pages = ceil($total_results / 10);
for ($i = 1; $i <= $total_pages; $i++) {
echo "<a href='search.php?page=$i'>$i</a> ";
}
?>
Keywords
Related Questions
- How can the use of setCurrentBlock() and parseCurrentBlock() functions in PHP impact the order of template blocks being parsed and displayed?
- What are the best practices for beginners to follow when working with PHP and XML parsing?
- In PHP file handling, how can discussing and sharing code with others lead to identifying and solving errors more effectively?