How can you ensure that the next or previous 12 entries are displayed when using the "LIMIT" function in MySQL queries for pagination?
When using the "LIMIT" function in MySQL queries for pagination, you can ensure that the next or previous 12 entries are displayed by adjusting the offset value based on the current page number. For example, to display the next 12 entries, you would set the offset to the current page number multiplied by 12. To display the previous 12 entries, you would set the offset to the current page number minus 1, multiplied by 12.
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$offset = ($page - 1) * 12;
$query = "SELECT * FROM table_name LIMIT 12 OFFSET $offset";
$result = mysqli_query($connection, $query);
while ($row = mysqli_fetch_assoc($result)) {
// Display the data
}
Related questions
-
PHP form submission button click
-
magic methods PHP classes class variables
-
PHP user information extraction