I am fairly new and incompetent when it comes to PHP, so my question might be dumb. Sorry if it is.
I'd like to have a pagination on my search results because anytime there are many results, the page loads really slow. The website is connected to a phpmyadmin database.
This is how my pagination coding looks like:
<?php
require 'partials/header.php'; //navigation and database connection
if (isset($_GET['search']) && isset($_GET['submit'])) {
$search = filter_var($_GET['search'], FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$query = "SELECT * FROM posts WHERE title LIKE '%$search%' ORDER BY date_time DESC";
$posts = mysqli_query($connection, $query);
}
//if i don't comment this one out for the pagination: Cannot modify header information - headers already sent
else {
header('location: ' . ROOT_URL . 'index.php');
die();
}
?>
//pagination
<?php
$searches_per_page = 20;
$number_of_searches = mysqli_num_rows($posts);
$number_of_pages = ceil($number_of_searches/$searches_per_page);
if(!isset($_GET['page'])) {
$page = 1;
} else {
$page = $_GET['page'];
}
$this_page_first_searches = ($page - 1) * $searches_per_page;
$query = "SELECT * FROM posts WHERE title LIKE '%$search%' ORDER BY date_time DESC LIMIT " . $this_page_first_searches . ', ' . $searches_per_page;
$posts = mysqli_query($connection, $query);
?>
The HTML part is 100% working correctly, the only issue that I'm struggling with is that everytime I switch to another page through my pagination, it gives an error with these messages:
I am sorry if my explanation is unclear, I tried my best.
So, the pagination gets displayed correctly, the first result page works well too, but as soon as I click to page 2, the warnings above are getting displayed.
I have no idea how to fix this issue. Does anyone have an idea what to do?