I've just started using http_build_query
. I've implemented it into a pagination script of mine however it's not working right.
What I am expecting is for my urls to be http://example.com/categories.php?cat=category&page=2
but for some reason it is adding 20
to my page numbers making the urls like so http://example.com/categories.php?cat=category&page=22 <<< This is actually for page 2
.
Here is my original script:
$img_start=0;
$img_limit=8;
if(isset($_GET['page'])) {
$page=$_GET['page'];
$img_start=($page-1)*$img_limit;
}
else { $page = 1; }
if($_COOKIE['age_verification'] == "adult") {
$img_total = mysqli_num_rows(mysqli_query($conn, "select * from gallery_img WHERE $cat = 1"));
}
else if($_COOKIE['age_verification'] == "child") {
$img_total = mysqli_num_rows(mysqli_query($conn, "select * from gallery_img WHERE $cat = 1 AND WHERE nude != 1"));
}
$img_total_count = ceil($img_total/$img_limit);
if($img_limit != $img_total) {
echo '<nav aria-label="Page navigation">
<ul class="pagination">' . PHP_EOL;
if($page>1) {
echo '<li><a href="?page='.($page-1).'" aria-label="Previous"><span aria-hidden="true">Previous</span></a></li>' . PHP_EOL;
}
for($i=1;$i<=$img_total_count;$i++) {
if($i==$page) { echo "<li class='active'><a href='?page=".$i."'>".$i."</a></li>" . PHP_EOL; }
else { echo "<li><a href='?page=".$i."'>".$i."</a></li>" . PHP_EOL; }
}
if($page!=$img_total_count) {
if(!isset($page)) { echo '<li><a href="?page='.($page+2).'" aria-label="Next"><span aria-hidden="true">Next</span></a></li>' . PHP_EOL; }
else { echo '<li><a href="?page='.($page+1).'" aria-label="Next"><span aria-hidden="true">Next</span></a></li>' . PHP_EOL; }
}
echo '</ul>
</nav>' . PHP_EOL;
}
And then these are the lines I modified which are adding 20
to my page numbers:
$pageParameters = http_build_query(array_merge($_GET, array("page"=>2)));
if($img_limit != $img_total) {
echo '<nav aria-label="Page navigation">
<ul class="pagination">' . PHP_EOL;
if($page>1) {
echo '<li><a href="'.htmlspecialchars("$_SERVER[PHP_SELF]?$pageParameters").($page-1).'" aria-label="Previous"><span aria-hidden="true">Previous</span></a></li>' . PHP_EOL;
}
for($i=1;$i<=$img_total_count;$i++) {
if($i==$page) { echo "<li class='active'><a href='".htmlspecialchars("$_SERVER[PHP_SELF]?$pageParameters").$i."'>".$i."</a></li>" . PHP_EOL; }
else { echo "<li><a href='".htmlspecialchars("$_SERVER[PHP_SELF]?$pageParameters").$i."'>".$i."</a></li>" . PHP_EOL; }
}
if($page!=$img_total_count) {
if(!isset($page)) { echo '<li><a href="'.htmlspecialchars("$_SERVER[PHP_SELF]?$pageParameters").($page+2).'" aria-label="Next"><span aria-hidden="true">Next</span></a></li>' . PHP_EOL; }
else { echo '<li><a href="'.htmlspecialchars("$_SERVER[PHP_SELF]?$pageParameters").($page+1).'" aria-label="Next"><span aria-hidden="true">Next</span></a></li>' . PHP_EOL; }
}
echo '</ul>
</nav>' . PHP_EOL;
}
Why would this be adding 20 to my page numbers?