Home > Back-end >  How to show first 3 and last 3 pages in php pagination
How to show first 3 and last 3 pages in php pagination

Time:09-22

I create php pagination for articles it shows all pages over 1k and getting longers.

I want to do something like this prev 1234 ... 9999 next.

I tried this answer enter image description here

CodePudding user response:

You can generate you pagination with

<ul class="pagination justify-content-end">
   <li class="page-item <?php if($page <= 1){ echo 'disabled'; } ?>">
      <a class="page-link" href="<?php if($page <= 1){ echo '#'; } else { echo "?page=" . $prev; } ?>">Geri</a>
   </li>
   <?php for($i = $page; $i <= $page   3; $i   ): ?>
   <?php
            if($page < $totoalPages - $i){ ?>
                <li class="page-item <?php if($page == $i) {echo 'active'; } ?>">
                    <a class="page-link" href="posts.php?page=<?php echo $i; ?>"> <?php echo $i; ?> </a>
                </li>
            <?php }
   ?>
   <?php endfor; ?>
   <li class="page-item">
      <span>....</span>
   </li>
   <?php for($i = 0; $i <= 3; $i   ): ?>
   <?php
            if($totoalPages > $page - 4 -  $i){ ?>
                <li class="page-item <?php if($page == $totoalPages - (4 - $i)) {echo 'active'; } ?>">
                    <a class="page-link" href="posts.php?page=<?php echo $totoalPages - (4 - $i); ?>"> <?php echo $totoalPages - (4 - $i); ?> </a>
                </li>
            <?php }
   ?>
   <?php endfor; ?>
   
   <li class="page-item <?php if($page >= $totoalPages) { echo 'disabled'; } ?>">
      <a class="page-link" href="<?php if($page >= $totoalPages){ echo '#'; } else {echo "?page=". $next; } ?>">İleri</a>
   </li>
</ul>
  • Related