PHP - pagination example

0 votes
242 views
added Aug 8, 2019 in PHP by lcjr First Warrant Officer (11,740 points)
<?php
  $pagination = '';
  $limitPerPage = 10;
  $total_pages = ceil($total/$limitPerPage);
  if(isset($_GET["page"])){
    $current_page = $_GET["page"];
  }else{
    $current_page = 1;
  }

  if($total_pages > 0 && $total_pages != 1 && $current_page <= $total_pages){ //verify total pages and current page number        
    $next           = $current_page + 1; //next link
    $first_link     = true; //boolean var to decide our first link
    // display prev
    if($current_page > 1){
      $previous_pg = $current_page-1;
      $pagination .= '<li><a href="/tags/'.end($tag).'?page='.$previous_pg.'" data-page="'.$previous_pg.'" title="Previous"><span aria-hidden="true"><i class="ion-icon ion-ios-arrow-back"></i></span></a></li>'; //previous link
      $first_link = false; //set first link to false
    }
    if($current_page==1){
      $pagination .= '<li><a id="prevPg" href="javascript:void(0);" data-page="1" title="" style="pointer-events:none"><span aria-hidden="true"><i class="ion-icon ion-ios-arrow-back"></i></span></a></li>';
    }
    // display current
    $i = $current_page;
    $pagination .= '<li class="active"><a href="javascript:void(0);" data-page="'.$i.'" title="Page '.$i.'" style="pointer-events:none">'.$i.'</a></li>';
    // display next
    if($current_page < $total_pages){
      $next_pg = $current_page+1;
      $pagination .= '<li><a href="/tags/'.end($tag).'?page='.$next_pg.'" data-page="'.$next_pg.'" title="Next"><span aria-hidden="true"><i class="ion-icon ion-ios-arrow-forward"></i></span></a></li>'; //next link
    }
    if($current_page==$total_pages){
      $pagination .= '<li><a id="nextPg" href="javascript:void(0);" data-page="'.$total_pages.'" title="" style="pointer-events:none"><span aria-hidden="true"><i class="ion-icon ion-ios-arrow-forward"></i></span></a></li>';
    }
  }
  echo $pagination;
?>

 

lazacode.org - Malaysia's programming knowledge sharing platform, where everyone can share their finding as reference to others.
...