每页项目 - 无法在一个页面中显示所有项目 - Php Pagination

时间:2016-09-09 10:33:25

标签: php pagination

我创建了一个下拉按钮,用于选择与我的php分页相关的每页项目,即$limit。 3,10,20和100的选择器工作正常。

当我点击All时,该页面仅显示default限制为10.该链接显示总数(320),因为它应该是至。但该页面只显示10个条目。

限制选择器:

<?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;
}
?>

下拉:

<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>

如果需要,以下是分页脚本:

<?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。我不是专业人士。我用PHP的有限知识创建了$limit代码。

1 个答案:

答案 0 :(得分:0)

你的开关正在寻找“全部”这个词,但是你正在给它别的东西:

<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;
}

如果您向limit提供的值不适合任何情况,则默认为10。

因此,更改下拉列表中的一行代码应该可以修复它。

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