0

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 Limit pagination page number but could not make it work.

codes :

  if(isset($_POST['records-limit'])){
      $CurrentPage['records-limit'] = $_POST['records-limit'];
  }
  
  $limit = isset($CurrentPage['records-limit']) ? $CurrentPage['records-limit'] : 10;
  $page = (isset($_GET['page']) && is_numeric($_GET['page']) ) ? $_GET['page'] : 1;
  $paginationStart = ($page - 1) * $limit;
  $authors = $db->getPosts("SELECT * FROM posts LEFT JOIN users ON posts.post_userId = users.user_id LIMIT $paginationStart, $limit")->fetchAll();

  // Get total records
  $sql = $db->getPosts("SELECT count(post_id) AS post_id FROM posts")->fetchAll();
  $allRecrods = $sql[0]['post_id'];
  
  // Calculate total pages
  $totoalPages = ceil($allRecrods / $limit);

  // Prev + Next
  $prev = $page - 1;
  $next = $page + 1;

Pagination

<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 = 1; $i <=$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 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>

2 Answers2

0

This is an example with a different code than what you suggested... we basically need to know how many pages we have and the current one... I assume page # is also /1 in the link here:

<?php
$totoalPages = 1195;
$currentPage = 4;

?>
<style>
div#pagination {
    display: flex;
}

</style>
<div id="pagination">
    <?php
    if ($currentPage > 1){
        ?>
            <a href="/popular/whatever.html/<?php echo $currentPage - 1;?>" title="Prev"><div class="ctrl_el">Prev&nbsp;</div></a>
        <?php
    } ?>
    <a href="/popular/whatever.html/<?php echo $currentPage;?>" title="Page <?php echo $currentPage;?>"><div class="ctrl_el"><?php echo $currentPage;?>&nbsp;</div></a>
    <a href="/popular/whatever.html/<?php echo $currentPage + 1;?>" title="Page <?php echo $currentPage + 1;?>"><div class="ctrl_el"><?php echo $currentPage + 1;?>&nbsp;</div></a>
    <a href="/popular/whatever.html/<?php echo $currentPage + 2;?>" title="Page <?php echo $currentPage + 2;?>"><div class="ctrl_el ctrl_sel"><?php echo $currentPage + 2;?>&nbsp;</div></a>
    <a href="/popular/whatever.html/<?php echo $currentPage + 3;?>" title="Page <?php echo $currentPage + 3;?>"><div class="ctrl_el"><?php echo $currentPage + 3;?>&nbsp;</div></a>
    <div class="ctrl_el_dot">....</div><a href="/popular/whatever.html/<?php echo $totoalPages;?>" title="Page <?php echo $totoalPages;?>"><div class="ctrl_el"><?php echo $totoalPages;?>&nbsp;</div></a>
    <?php
    if ($totoalPages  > $currentPage){
        ?>
            <a href="/popular/whatever.html/<?php echo $currentPage + 1;?>" title="Next"><div class="ctrl_el">Next</div></a>
        <?php
    } ?>

</div>

I added the style just for the display to show them side by side... I see no reason for a loop but its all the same to do it with a loop :)

This will return:

enter image description here

Shlomtzion
  • 674
  • 5
  • 12
0

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>
Vivek Choudhary
  • 634
  • 8
  • 14
  • Thanks for the answer, its show 1 page less than total pages. for example if I have 1000 total pages it shows **prev 12345 ... 999 next** it should be ** prev 12345 ... 996,997,998,999,1000 next**. I am looking in codes to see where it drops. – Backlink TR Sep 22 '21 at 13:29