drc15469 2017-04-29 00:55
浏览 53
已采纳

如何在以下代码中设置条件以在分页中标记当前页面?

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.

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>
  • 写回答

2条回答 默认 最新

  • drcrc28428 2017-04-29 01:24
    关注

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

    to:

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

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable