Okay so I can’t seem to make this pagination work. I mean it works. It shows data like I want it to, but what I’m trying to achieve is allow only up to 20 data to be pulled from the database per page. Well, it does do that, but this is what it also does.
This is the first pagination. So the URL and everything works. The URL is ?page=2. This page is fine since it’s doing what I asked. List #1 data - #20 data.
Now here’s where it’s acting weird on me. It lists 20 datas like I asked, but for the first data of the page, it shows the last data from the previous page. So 20th comment actually shows on ?page=3.
Here, it’s taking 2 datas from the previous page. So from the original ?page=2, it pulled 2 datas into ?page=4 from both ?page=2 and ?page=3
Here’s my code.
$GET_URL_PAG_DATABASE = new mysqli("localhost", "username", "password", "database");
if($GET_URL_PAG_DATABASE->connect_errno) {
echo "Error, failed to connect to MySQL database. Please fix.";
exit();
}
$page = isset($_GET['page']) ? (int) $_GET['page'] : 1;
$pages = implode(mysqli_fetch_assoc($GET_URL_PAG_DATABASE->prepare("SELECT COUNT(id) FROM comment WHERE parent_id = ?")));
$pages = ceil($pages / 20);
$querystring = "";
foreach($_GET as $key => $value) {
if($key != "page") $querystring .= "$key=$value&";
}
$GET_URL_PAG = $GET_URL_PAG_DATABASE->prepare("SELECT id, parent_id, message FROM comment WHERE parent_id = ? ORDER BY id ASC LIMIT " . (($page - 1) * 19) . ", 20");
$GET_URL_PAG->bind_param("s", $GET_ID);
$GET_ID = filter_var($_GET['id'], FILTER_SANITIZE_URL);
$GET_URL_PAG->execute();
$GET_URL_PAG->bind_result($GET_COMMENT_ID, $GET_PARENT_ID, $GET_MESSAGE);
while($GET_URL_PAG->fetch()) {
echo "ID Number: " . $GET_COMMENT_ID . ": " . $GET_MESSAGE . "<br />";
}
I know that it has something to do with " . (($page - 1) * 19) . “, 20” but if I change 19 into 20, it’ll pull only never include the first ever data in ?page=2. So if the message is “1st comment”, it’ll only show “2nd comment” and not “1st comment”. If I change $page - 1 to $page - 2, it do the same thing, but going forwards, if I change it to $page - 0, it won’t show anything.
I need some guidance. Also, sorry if all my variables are in caps, I normally like to keep my stuff in caps so that I know where everything is.