Reputation: 1039
So I have a simple pagination system on my website. But there's a problem with my calculation for the next page. whenever the page is on page 2 the next page is 13 instead of 3. I have tested the calculation and it always comes out with 3 as result. Here is my code:
<?php
$applied_filters = array("parent" => null, "child" => null);
if(isset($_GET['cat'])) {
$applied_filters["parent"] = $_GET['cat'];
if(isset($_GET['sub']))
$applied_filters["child"] = $_GET['sub'];
}
if(isset($_GET['page'])) {
$page = $_GET['page'];
} else {
$page = 1;
}
$products_per_page = 9;
$start_from = ($page-1) * $products_per_page;
$pagination_url = "winkel.php?";
if($applied_filters["parent"] != null) {
$pagination_url .= "cat=" . $applied_filters["parent"];
if($applied_filters["child"] != null) {
$pagination_url .= "&sub=" . $applied_filters["child"] . "&page=";
} else {
$pagination_url .= "&page=";
}
} else {
$pagination_url .= "page=";
}
echo $page . '<br>'; // shows 2
$nextpage = $page + 1; //shows 3
echo $nextpage;
?>
<ul class="pagination">
<?php if($page > 1) { ?>
<li><a href="<?= $pagination_url .= $page - 1; ?>">«</a>
</li>
<?php } ?>
<li class="active"><a href="#"><?= $page ?></a>
</li>
<?php if($page < $max_pages) { echo $nextpage; // shows 3?>
<li><a href="<?= $pagination_url .= $nextpage; // shows 13 ?>">»</a>
</li>
<?php } ?>
I also have some URL code for the filters on my website:
<?php
// link filters
if(isset($_GET['cat'])) {
if(isset($_GET['cat']) && isset($_GET['sub'])) {
if($_GET['cat'] != "alles")
$result_products = $db->get_by_cat("products", $_GET['cat'], $_GET['sub'], $start_from, $products_per_page);
} else if (isset($_GET['cat'])) {
if($_GET['cat'] != "alles")
$result_products = $db->get_by_cat("products", $_GET['cat'], null, $start_from, $products_per_page);
}
}
if(isset($_POST['clearbrand'])) {
unset($_POST['brandfilter']);
unset($_POST['applybrand']);
}
$filtered_brands = null;
if(isset($_POST['brandfilter'])) $filtered_brands = $_POST['brandfilter'];
// form filters
if(isset($_POST['applybrand'])) {
if(isset($_GET['cat'])) {
$result_products = $db->get_by_checkbox("products", "brand", $_POST['brandfilter'], $_GET);
} else {
$result_products = $db->get_by_checkbox("products", "brand", $_POST['brandfilter']);
}
}
?>
So, does anybody know why my pagination counts from 2 to 13? Thanks in advance
Upvotes: 0
Views: 46
Reputation: 11328
Your problem is here:
<?php if($page > 1) { ?>
<li><a href="<?= $pagination_url .= $page - 1; ?>">«</a></li>
<?php } ?>
<li class="active"><a href="#"><?= $page ?></a></li>
<?php if($page < $max_pages) { echo $nextpage; // shows 3?>
<li><a href="<?= $pagination_url .= $nextpage; // shows 13 ?>">»</a></li>
<?php } ?>
If $page
is greater than 1 (which it is on page 2), you add $page - 1
to the $pagination_url
, basically putting a "1" at the end.
Then, in the second if
statement, if $page
is less than $max_pages
, you also add $nextpage
(which is 3) to the $pagination_url
, putting a "3" after the "1" you added before. Therefore, $pagination_url
will now have "13" at the end.
You'll want to change this to:
<?php if($page > 1) { ?>
<li><a href="<?= $pagination_url . ($page - 1); ?>">«</a></li>
<?php } ?>
<li class="active"><a href="#"><?= $page ?></a></li>
<?php if($page < $max_pages) { echo $nextpage; // shows 3?>
<li><a href="<?= $pagination_url . $nextpage; // shows 13 ?>">»</a></li>
<?php } ?>
Upvotes: 2