dngs dngs - 2 months ago 12
PHP Question

Items Per Page - Can't show all items one a single page - Php Pagination

I have created a dropdown button to select Items per Page i.e.

$limit
related to my php pagination. The selectors for 3, 10, 20 and 100 work fine.

But when I click on
All
, the page only shows the
default
limit which is 10. The link displays total number of entries (320) as it is supposed to. But the page only shows 10 entries.

Limit Selector:

<?php
switch($_GET["limit"]){
case "3": $limit = 3; break;
case "10": $limit = 10; break;
case "20": $limit = 20; break;
case "100": $limit = 100; break;
case "All": $limit = $rows1; break;
default: $limit = 10; break;
}
?>


The Dropdown:

<div class="w3-dropdown-click">
<button onclick="myFunction()" class="w3-btn w3-deep-orange">Items Per Page</button>
<div id="Demo" class="w3-dropdown-content w3-card-4">
<a href="tips.php?page=<?php echo $page; ?>&limit=3">3</a>
<a href="tips.php?page=<?php echo $page; ?>&limit=10">10</a>
<a href="tips.php?page=<?php echo $page; ?>&limit=20">20</a>
<a href="tips.php?page=<?php echo $page; ?>&limit=100">100</a>
<a href="tips.php?page=<?php echo $page; ?>&limit=<?php echo $rows1; ?>">All</a>
</div>
</div>

<script>
function myFunction() {
var x = document.getElementById("Demo");
if (x.className.indexOf("w3-show") == -1) {
x.className += " w3-show";
} else {
x.className = x.className.replace(" w3-show", "");
}
}
</script>


Here is the pagination script in case if it is needed:

<?php
$pagination = "";

if($lastpage > 1)
{
$pagination .= "<ul class=\"w3-pagination w3-white w3-xlarge w3-card-8 w3-hide-small\">";

//previous button
if ($page > 1)
$pagination.= "<li><a href=\"$targetpage?page=$prev&limit=$limit\" class=\"w3-hover-theme\"><i class=\"fa fa-arrow-left\"></i></a></li>";
else
$pagination.= "<li><a class=\"w3-deep-orange\"><i class=\"fa fa-arrow-left\"></i></a></li>";

//Pages

//not enough pages to bother breaking it up
if ($lastpage < 5 + ($adjacents * 2))
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<li><a class=\"w3-deep-orange\">$counter</a></li>";
else
$pagination.= "<li><a href=\"$targetpage?page=$counter&limit=$limit\" class=\"w3-hover-theme\">$counter</a></li>";
}
}
//enough pages to hide some
elseif($lastpage > 5 + ($adjacents * 2))
{
//close to beginning; only hide later pages
if($page < 1 + ($adjacents * 2))
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $page)
$pagination.= "<li><a class=\"w3-deep-orange\">$counter</a></li>";
else
$pagination.= "<li><a href=\"$targetpage?page=$counter&limit=$limit\" class=\"w3-hover-theme\">$counter</a></li>";
}
$pagination.= "<li><a href=\"#\">...</a></li>";
$pagination.= "<li><a href=\"$targetpage?page=$lastpage&limit=$limit\" class=\"w3-hover-theme\">$lastpage</a></li>";
}
//in middle; hide some front and some back
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination.= "<li><a href=\"$targetpage?page=1&limit=$limit\" class=\"w3-hover-theme\">1</a></li>";
$pagination.= "<li><a href=\"#\">...</a></li>";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pagination.= "<li><a class=\"w3-deep-orange\">$counter</a></li>";
else
$pagination.= "<li><a href=\"$targetpage?page=$counter&limit=$limit\" class=\"w3-hover-theme\">$counter</a></li>";
}
$pagination.= "<li><a href=\"#\">...</a></li>";
$pagination.= "<li><a href=\"$targetpage?page=$lastpage&limit=$limit\" class=\"w3-hover-theme\">$lastpage</a><li>";
}
//close to end; only hide early pages
else
{
$pagination.= "<li><a href=\"$targetpage?page=1&limit=$limit\" class=\"w3-hover-theme\">1</a></li>";
$pagination.= "<li><a href=\"#\">...</a></li>";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<li><a class=\"w3-deep-orange\">$counter</a></li>";
else
$pagination.= "<li><a href=\"$targetpage?page=$counter&limit=$limit\" class=\"w3-hover-theme\">$counter</a></li>";

}
}
}

//next button
if ($page < $counter - 1)
$pagination.= "<li><a href=\"$targetpage?page=$next&limit=$limit\" class=\"w3-hover-theme\"><i class=\"fa fa-arrow-right\"></i></a></li>";
else
$pagination.= "<li><a class=\"w3-deep-orange\"><i class=\"fa fa-arrow-right\"></i></a></li>";
$pagination.= "</ul>\n";
}
?>
<?=$pagination?>


P.S. I am not a pro. I have created
$limit
code with my limited knowledge of PHP.

Answer

your switch is looking for the word: "all", but you are giving it something else:

<a href="tips.php?page=<?php echo $page; ?>&limit=<?php echo $rows1; ?>">All</a>

switch($_GET["limit"]){
  case "3": $limit = 3; break;
  case "10": $limit = 10; break;
  case "20": $limit = 20; break;
  case "100": $limit = 100; break;
  case "All": $limit = $rows1; break;
  default: $limit = 10; break;
}

When the value you gave to limit doesn't fit any of the cases, it defaults to 10.

So, changing that one line of code in the dropdown should fix it.

<a href="tips.php?page=<?php echo $page; ?>&limit=All">All</a>

Comments