Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 61199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:16:54+00:00 2026-05-10T18:16:54+00:00

I’m looking for an example algorithm of smart pagination. By smart, what I mean

  • 0

I’m looking for an example algorithm of smart pagination. By smart, what I mean is that I only want to show, for example, 2 adjacent pages to the current page, so instead of ending up with a ridiculously long page list, I truncate it.

Here’s a quick example to make it clearer… this is what I have now:

Pages: 1 2 3 4 [5] 6 7 8 9 10 11 

This is what I want to end up with:

Pages: ... 3 4 [5] 6 7 ... 

(In this example, I’m only showing 2 adjacent pages to the current page)

I’m implementing it in PHP/Mysql, and the ‘basic’ pagination (no trucating) is already coded, I’m just looking for an example to optimize it… It can be an example in any language, as long as it gives me an idea as to how to implement it…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T18:16:54+00:00Added an answer on May 10, 2026 at 6:16 pm

    Here is some code based on original code from this very old link. It uses markup compatible with Bootstrap’s pagination component, and outputs page links like this:

    [1] 2 3 4 5 6 ... 100 1 [2] 3 4 5 6 ... 100 ... 1 2 ... 14 15 [16] 17 18 ... 100 ... 1 2 ... 97 [98] 99 100 
    <?php  // How many adjacent pages should be shown on each side? $adjacents = 3;  //how many items to show per page $limit = 5;  // if no page var is given, default to 1. $page = (int)$_GET["page"] ?? 1;  //first item to display on this page $start = ($page - 1) * $limit;  /* Get data. */ $data = $db     ->query("SELECT * FROM mytable LIMIT $start, $limit")     ->fetchAll();  $total_pages = count($data);  /* Setup page vars for display. */ $prev = $page - 1; $next = $page + 1; $lastpage = ceil($total_pages / $limit); //last page minus 1 $lpm1 = $lastpage - 1;  $first_pages = "<li class='page-item'><a class='page-link' href='?page=1'>1</a></li>" .     "<li class='page-item'><a class='page-link' href='?page=2'>2</a>";  $ellipsis = "<li class='page-item disabled'><span class='page-link'>...</span></li>";  $last_pages = "<li class='page-item'><a class='page-link' href='?page=$lpm1'>$lpm1</a></li>" .     "<li class='page-item'><a class='page-link' href='?page=$lastpage'>$lastpage</a>";  $pagination = "<nav aria-label='page navigation'>"; $pagincation .= "<ul class='pagination'>";  //previous button  $disabled = ($page === 1) ? "disabled" : ""; $pagination.= "<li class='page-item $disabled'><a class='page-link' href='?page=$prev'>« previous</a></li>";  //pages  //not enough pages to bother breaking it up if ($lastpage < 7 + ($adjacents * 2)) {      for ($i = 1; $i <= $lastpage; $i++) {         $active = $i === $page ? "active" : "";         $pagination .= "<li class='page-item $active'><a class='page-link' href='?page=$i'>$i</a></li>";     } } elseif($lastpage > 5 + ($adjacents * 2)) {     //enough pages to hide some     //close to beginning; only hide later pages     if($page < 1 + ($adjacents * 2)) {         for ($i = 1; $i < 4 + ($adjacents * 2); $i++) {             $active = $i === $page ? "active" : "";             $pagination .= "<li class='page-item $active'><a class='page-link' href='?page=$i'>$i</a></li>";         }         $pagination .= $ellipsis;         $pagination .= $last_pages;     } elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)) {         //in middle; hide some front and some back         $pagination .= $first_pages;         $pagination .= $ellipsis         for ($i = $page - $adjacents; $i <= $page + $adjacents; $i++) {             $active = $i === $page ? "active" : "";             $pagination .= "<li class='page-item $active'><a class='page-link' href='?page=$i'>$i</a></li>";         }         $pagination .= $ellipsis;         $pagination .= $last_pages;     } else {         //close to end; only hide early pages         $pagination .= $first_pages;         $pagination .= $ellipsis;         $pagination .= "<li class='page-item disabled'><span class='page-link'>...</span></li>";         for ($i = $lastpage - (2 + ($adjacents * 2)); $i <= $lastpage; $i++) {             $active = $i === $page ? "active" : "";             $pagination .= "<li class='page-item $active'><a class='page-link' href='?page=$i'>$i</a></li>";         }     } }  //next button $disabled = ($page === $last) ? "disabled" : ""; $pagination.= "<li class='page-item $disabled'><a class='page-link' href='?page=$next'>next »</a></li>";  $pagination .= "</ul></nav>";  if($lastpage <= 1) {     $pagination = ""; }   echo $pagination;  foreach ($data as $row) {     // display your data }  echo $pagination;  
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 106k
  • Answers 106k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Windows Workflow Foundation feels like an overkill to me in… May 11, 2026 at 8:54 pm
  • Editorial Team
    Editorial Team added an answer The implemented solution is as follows. I have one jar… May 11, 2026 at 8:54 pm
  • Editorial Team
    Editorial Team added an answer Edit: I googled to check my answer: "Processing Data Queues… May 11, 2026 at 8:54 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.