如何在以下代码中设置条件以在分页中标记当前页面? [英] How to set condition in following code for marking current page in pagination?

查看:97
本文介绍了如何在以下代码中设置条件以在分页中标记当前页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为php- mysql分页设置条件,以便可以将当前页面" li "更改为" li class =" active""标记当前选中的页面.我是php的新手,正在搜索这种分页教程,但是没有运气.

I am trying to set a condition for php- mysql pagination so that it can change the current page "li" to "li class="active" " to mark the current selected page. I am new to php and searching for such pagination tutorial but no luck.

到目前为止,我已经完成了所有工作,但无法标记所选页面. $ id用于检测当前页面ID.如何设置条件(或其他条件),以便可以在分页中标记当前页面项目?数千感谢您的帮助.

I have done so far what is working but not able to mark selected page. Here $id is for detecting the current page id. How can I set if condition ( or other) so that I can mark the current page item in the pagination? Thousands thanks for helping me.

   <ul class="pagination">
     <?php if($id > 1) {?> <li><a href="?id=<?php echo ($id-1) ?>">Previous</a></li><?php }?>
     <?php
     for($i=1;$i <= $page;$i++){
     ?>

     <?php
     if ($id>1)
     { ?>
         <li class="active"><a href="?id=<?php echo $i ?>"><?php echo $i;?></a></li>
    <?php }
     ?>
    <!--     <li><a  href="?id=<?php echo $i ?>"><?php echo $i;?></a></li>  -->
      <?php
     }
      ?>
    <?php if($id!=$page)
    //3!=4
    {?> 
      <li><a href="?id=<?php echo ($id+1); ?>">Next</a></li>
    <?php }?>
 </ul>

推荐答案

您可以从以下位置更改for循环

You could change your for loop from

 <?php
     for($i=1;$i <= $page;$i++){
     ?>

     <?php
     if ($id>1)
     { ?>
         <li class="active"><a href="?id=<?php echo $i ?>"><?php echo $i;?></a></li>
    <?php }
     ?>
    <!--     <li><a  href="?id=<?php echo $i ?>"><?php echo $i;?></a></li>  -->
      <?php
     }
      ?>

收件人:

<?php
for($i=1;$i <= $page;$i++){
  $class=($i==$id)? ' class="active"' : '';
  echo '<li'.$class.'><a href="?id='.$i.'">'.$i.'</a></li>';
}
?>

如果我正确理解了您的代码,则$ page代表总页面,而$ id代表当前页面,这会将当前页面编号设置为活动类别,而其他页面则不包含该类别

If I've understood your code properly, $page represents total pages and $id represents the current page, this will set the current page number as the active class and leave the other pages without the class

这篇关于如何在以下代码中设置条件以在分页中标记当前页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆