Function to generate a pagination link list in PHP

When I was writing my blog software code, I came across a challenge peculiar to website scripts which require to display several pages of data. While actually retrieving the data for a particular page is simple enough using SQL queries (using SQL SELECT's LIMIT and OFFSET mechanism) generating a list of navigable links for each page of data is another cup of tea. Let's assume that you want to generate a list of pages in PHP. E.g. You need to create a blog navigation toolbar, with links to each page on the footer. The assumption is that you have hundreds of pages to display and you cannot display each page in the navigation toolbar, because that would become unwieldy. What you want is actually a list looking like this below (bolded item is current page):
Page: 1 2 3 4 5 ... 20
Or if the page is in the middle of the the navigation
1 ... 10 11 12 13 14 15 ... 20
If the page is at the end of this list
1 ... 12 13 14 15 16 17 18 19 20
This navigation scheme is not perfect, but it is adequate for most purposes. The following PHP function creates an array of navigable links which can be easily used to create such a pagination. This can be used as a generic pagination function, because it accepts all necessary parameters (including the base URL as well as the query string to use), total number of pages, current page and pagination limit. The pagination limits how many pages are actually shown around the current page.

<?php // Function to generate pagination array - that is a list of links // for pages navigation function paginate ($base_url, $query_str, $total_pages, $current_page, $paginate_limit) { // Array to store page link list $page_array = array (); // Show dots flag - where to show dots? $dotshow = true; // walk through the list of pages for ( $i = 1; $i <= $total_pages; $i ++ ) { // If first or last page or the page number falls // within the pagination limit // generate the links for these pages if ($i == 1 || $i == $total_pages || ($i >= $current_page - $paginate_limit && $i <= $current_page + $paginate_limit) ) { // reset the show dots flag $dotshow = true; // If it's the current page, leave out the link // otherwise set a URL field also if ($i != $current_page) $page_array[$i]['url'] = $base_url . "?" . $query_str . "=" . $i; $page_array[$i]['text'] = strval ($i); } // If ellipses dots are to be displayed // (page navigation skipped) else if ($dotshow == true) { // set it to false, so that more than one // set of ellipses is not displayed $dotshow = false; $page_array[$i]['text'] = "..."; } } // return the navigation array return $page_array; } ?>
To use the function in a PHP page, you just have to simply walk through the array:


<?php // To use the pagination function in a // PHP script to display the list of links // paginate 100 pages - current page is 50 and show // 5 links around the current page $pages = paginate ("myurl.php", "page", 100, 50, 5); ?> <p>Pages: <?php // list display foreach ($pages as $page) { // If page has a link if (isset ($page['url'])) { ?> <a href="<?php echo $page['url']?>"> <?php echo $page['text'] ?> </a> <?php } // no link - just display the text else echo $page['text']; } ?> </p>
The output of the above code will be
Pages: 1 ... 15 16 17 18 19 20 21 22 23 24 25 ... 30


1 Comments for "Function to generate a pagination link list in PHP"

Thank man, i very need this code pagination

Back To Top