How to set condition in following code for marking

2019-08-02 17:13发布

问题:

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>

回答1:

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



回答2:

Assuming that $id is the current page number, and that $page is the total number of pages, you need to highlight only one by doing the following in your loop:

  • if($i==$id) // highlight
  • else // don’t highlight

Your main errors are that you didn’t test whether $i==$id, and you didn’t have an alternative unhighlighted version.

I really think that you should simplify your code by separating your logic from the HTML. It becomes very unreadable otherwise, and very hard to manage.

I have taken the liberty of doing just that. This way you can see where the logic does the hard work, an you only need to print the results in the HTML.

<?php
    $id=3;  //  test value
    $page=20;   //  test value

    $li=array();    //  temporary array for convenience
    $template='<li%s><a href="?id=%s">%s</a></li>';
    if($id>1) $li[]=sprintf($template,'',$id-1,'Previous');
    for($i=1;$i<=$page;$i++) {
        if($i==$id) $li[]=sprintf($template,' class="active"',$i,$i);   //  Current
        else $li[]=sprintf($template,'',$i,$i);
    }
    if($id<$page) $li[]=sprintf($template,'',$id+1,'Next');
    $li=implode('',$li); // convert to string for printing
?>
<ul class="pagination">
<?php print $li; ?>
</ul>

You will also see two techniques to make things easier:

  • I use an array as a temporary container. It is easier to add items this way and then to implode it into a string afterwards
  • sprintf makes it easier to define a template string, so you can manage the HTML component more easily, and fill in the gaps when the time comes.