Pagination Function

function pagination($item_count, $limit, $cur_page, $link)
       $page_count = ceil($item_count/$limit);
       $current_range = array(($cur_page-2 < 1 ? 1 : $cur_page-2), ($cur_page+2 > $page_count ? $page_count : $cur_page+2));

       // First and Last pages
       $first_page = $cur_page > 3 ? '<a href="'.sprintf($link, '1').'">1</a>'.($cur_page < 5 ? ', ' : ' ... ') : null;
       $last_page = $cur_page < $page_count-2 ? ($cur_page > $page_count-4 ? ', ' : ' ... ').'<a href="'.sprintf($link, $page_count).'">'.$page_count.'</a>' : null;

       // Previous and next page
       $previous_page = $cur_page > 1 ? '<a href="'.sprintf($link, ($cur_page-1)).'">Previous</a> | ' : null;
       $next_page = $cur_page < $page_count ? ' | <a href="'.sprintf($link, ($cur_page+1)).'">Next</a>' : null;

       // Display pages that are in range
       for ($x=$current_range[0];$x <= $current_range[1]; ++$x)
               $pages[] = '<a href="'.sprintf($link, $x).'">'.($x == $cur_page ? '<strong>'.$x.'</strong>' : $x).'</a>';

       if ($page_count > 1)
               return '<p class="pagination"><strong>Pages:</strong> '.$previous_page.$first_page.implode(', ', $pages).$last_page.$next_page.'</p>';


   total amount of item/rows/whatever,
   limit of items per page,
   current page number,


pagination(45, 20, 1, '');

Output HTML:

<p class="pagination">
   <a href=""><strong>1</strong></a>,
   <a href="">2</a>,
   <a href="">3</a>
   | <a href="">Next</a>


  1. User Avatar
    Permalink to comment#

    Code in the first codeblock is corrupted. The code for the html links are not visible.

  2. User Avatar
    Permalink to comment#

    thanks. great wor!

  3. User Avatar
    Saqib Ansari
    Permalink to comment#

    Code is not working…….

  4. User Avatar

    I am going through some seo problems with the paginated pages on my site. How do i block or disable pagination altogether. Here is an example of a page
    I have lots like this, and it is a seo nightmare


  5. User Avatar

    haahahahaha… I like you don’t say a word of instruction in this….

  6. User Avatar
    Permalink to comment#

    A little help sir?

  7. User Avatar
    The Internet
    Permalink to comment#

    Its a snippet, not a tutorial. If you can’t understand the code then don’t use it.
    Its that simple.


  8. User Avatar
    Important Coupon
    Permalink to comment#

    Thanks for your useful code. It’ll help many junior programmer.

  9. User Avatar
    Permalink to comment#

    Thanks! I didnt feel like writing another one of these, and yours works great!

  10. User Avatar
    Permalink to comment#

    Works well. You can also change the page linking in the for loop to:

    $pages[] = ($x == $page ? '<strong>'.$x.'</strong>' : '<a href="'.sprintf($link, $x).'" rel="nofollow">'.$x.'</a>');

    So that the page number you’re currently on isn’t linked.

Leave a Comment

Posting Code!

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.