简体   繁体   English

PHP分页IF当前页面

[英]PHP Pagination IF Current Page

I have got this pagination I'm got working using PHP, it works great, but while making this work, I've forgot that the user will need to know which page they are currently on. 我已经使用PHP进行了分页,效果很好,但是在进行此工作时,我忘记了用户将需要知道他们当前在哪个页面上。

Would there be an easy way to add to this so if the page number is the same as $_GET['page'] I can add a new class to the div? 是否有一种简单的方法可以添加到此列表中,因此,如果页码与$_GET['page']相同,则可以向div添加新类? I have tried doing another if statement but haven't managed to get anything working, so any guidance would be great! 我已经尝试过执行另一个if语句,但是没有成功进行任何工作,因此任何指导都很棒!

  <?php
    $totalpage      = $values2->SearchPropertiesPagingTotalPropertiesResult / $_GET['perpage'];
    $currentpage    = (isset($_GET['page']) ? $_GET['page'] : 1);
    $firstpage      = 1;
    $lastpage       = ceil($totalpage);
    $loopcounter = ( ( ( $currentpage + 2 ) <= $lastpage ) ? ( $currentpage + 2 ) : $lastpage );
    $startCounter =  ( ( ( $currentpage - 2 ) >= 3 ) ? ( $currentpage - 2 ) : 1 );

    if($totalpage > 1)
                    {
                        $pagination .= '<div class="text-center m-t-40"><nav><ul class="pagination pagination-lg">';
                        $pagination .= '<li ><a href="#" aria-label="Previous Page"><i class="fa fa-angle-left" aria-hidden="true"></i></span></a></li>';
                        for($i = $startCounter; $i <= $loopcounter; $i++)
                        {

                            $pagination .= '<li><a href="'. $agencyurl .'/search/'. $_REQUEST['bed'] .'/'. $i .'/'. $_REQUEST['perpage'] .'/'. $_REQUEST['area'] .'">'. $i .'</a></li>';
                        }
                        $pagination .= '<li ><a href="#" aria-label="Next Page"><i class="fa fa-angle-right" aria-hidden="true"></i></a></li>';
                        $pagination .= '</ul></nav></div>';
                    }
                echo $pagination;
    ?>

Inside your for loop, break the opening of the <li> and then continue: 在您的for循环中,断开<li>的开头,然后继续:

for($i = $startCounter; $i <= $loopcounter; $i++)
{
    $pagination .= '<li '
    if ($i == $currentpage) $pagination .= 'class=".current"';
    $pagination .= '><a href="'. [...]
}

Now all you need to do is to style your new .current class in your CSS. 现在,您需要做的就是在CSS中设置新的.current类的样式。

for($i = $startCounter; $i <= $loopcounter; $i++)
{
if ($_GET['page'] == $i) {
     $pagination .= '<li><span class="current">' . $i . '</span></li>';
} else {
    $pagination .= '<li><a href="'. $agencyurl .'/search/'. $_REQUEST['bed'] .'/'. $i .'/'. $_REQUEST['perpage'] .'/'. $_REQUEST['area'] .'">'. $i .'</a></li>';
  }
}

Adding an IF condition inside the loop will let you control the pagination design. 在循环内添加IF条件将使您可以控制分页设计。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM