So basically i created a webpage with url:
http://localhost/bbe/alumni/showthread.php?t=19
$limit = 8;
$offset = (isset($_GET["page"]) ? $_GET["page"] - 1 : 0) * $limit;
Then put a comment section (with paging) but my codes for paging is not working
<?php
$total = $dbcon->query("SELECT count(*) FROM comment") or die(mysqli_error());
$fetch = $total->fetch_assoc();
for($x = 0; $x < $fetch["count(*)"] / $limit ; $x ++)
{
$page = $x + 1;
if((isset($_GET["page"]) ? $_GET["page"] : 1) == $page) $page = "<b>".$page."</b>";
echo '<a href="showthread.php?t='.$id.'?page='.($x + 1).'" class="label label-danger">'.$page.'</a> ';
}
?>
after 8 comments there is the page number but when i clicked the link for page 2 nothing happens except for the url is changed to:
http://localhost/bbe/alumni/showthread.php?t=19?page=2
and also if i clicked back to page 1 the url changed to
http://localhost/bbe/alumni/showthread.php?t=19?page=2?page=1
and so on.. why does the link in my paging doesn't work? sorry im new in php and still learning online
As I already commented, additional request variables need to be separated by &
, not ?
. So the url should become http://localhost/bbe/alumni/showthread.php?t=19&page=2
.
The correct code for building the link should probably look like this:
echo '<a href="showthread.php?t='.$id.'&page='.($x + 1).'" class="label label-danger">'.$page.'</a> ';
Or, while I'm at it, a bit cleaner imo would be something like this:
$requestedPage = isset($_GET["page"]) ? $_GET["page"] : 1;
for($page = 1; $page <= $fetch["count(*)"] / $limit; $page++) {
// active page does not need to be a link
if($requestedPage == $page) {
echo "<strong>$page</strong>";
continue;
}
echo "<a href='showthread.php?t=$id&page=$page' class='label label-danger'>$page</a>";
}
Using LIMIT
you can limit the number of rows to display per page, by setting limit
and offset
.
$total = $dbcon->query("SELECT count(*) FROM comment") or die(mysqli_error());
$num_rows = $total->fetch_assoc()["count(*)"]; // get row count
$limit = 8;
$lastPage = round(($num_rows / $limit) - 1); // get last page
$current_page = isset($_GET['t']) ? $_GET['t'] : 0; // if not set the page is index.php
$offset = $current_page * $limit; // get offset by multiplying the current page number by limit
$sql = "SELECT * FROM comment LIMIT $offset, $limit";
$result = $dbcon->query($sql); // get results using LIMIT
// display results here
while ($row = $result->fetch_assoc()) {
// echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
echo createNav($current_page, $lastPage); // creating pagination
function createNav($pageNumber, $lastPage)
{
$listItems = '';
while ($lastPage >= 1) {
$pageNumber += 1;
if ($pageNumber >= 1) {
$listItems = createListItem($pageNumber, $lastPage) . $listItems;
}
$lastPage -= 1;
}
return $listItems;
}
function createListItem($pageNumber, $lastPage)
{
if ($pageNumber <= $lastPage && $lastPage > 0) {
return '<a href="showthread.php?t=' . $pageNumber . '?&page=' . ($pageNumber + 1) . '">' . $pageNumber . '</a><br>';
}
}