How can pagination be implemented in PHP to display a certain number of users per page in a buddy list?
To implement pagination in PHP to display a certain number of users per page in a buddy list, you can use the LIMIT clause in SQL queries to fetch a specific number of users per page. Additionally, you can calculate the total number of pages based on the total number of users and the desired number of users per page. Then, you can create navigation links to allow users to navigate between pages.
<?php
// Assuming $page is the current page number and $usersPerPage is the number of users to display per page
$offset = ($page - 1) * $usersPerPage;
// Query to fetch users with pagination
$query = "SELECT * FROM users LIMIT $offset, $usersPerPage";
$result = mysqli_query($connection, $query);
// Calculate total number of pages
$totalUsers = mysqli_num_rows(mysqli_query($connection, "SELECT * FROM users"));
$totalPages = ceil($totalUsers / $usersPerPage);
// Display users
while ($row = mysqli_fetch_assoc($result)) {
echo $row['username'] . "<br>";
}
// Display pagination links
for ($i = 1; $i <= $totalPages; $i++) {
echo "<a href='buddy_list.php?page=$i'>$i</a> ";
}
?>