PHP and MySQL pagination update help

It has been some time since I updated my pagination on my web page, and I am trying to add First and Last Links to my pagination, as well ... when the search results are long. For example, I am trying to do the following in the example below. Can someone help me fix my code so that I can update my site. Thanks

 First Previous 1 2 3 4 5 6 7 ... 199 200 Next Last 

Currently, my code displays the following.

 Previous 1 2 3 4 5 6 7 Next 

Here is part of my pagination code that displays links.

 if ($pages > 1) { echo '<br /><p>'; $current_page = ($start/$display) + 1; if ($current_page != 1) { echo '<a href="index.php?s=' . ($start - $display) . '&p=' . $pages . '">Previous</a> '; } for ($i = 1; $i <= $pages; $i++) { if ($i != $current_page) { echo '<a href="index.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '">' . $i . '</a> '; } else { echo '<span>' . $i . '</span> '; } } if ($current_page != $pages) { echo '<a href="index.php?s=' . ($start + $display) . '&p=' . $pages . '">Next</a>'; } echo '</p>'; } 
+4
source share
5 answers

In the interest of not reinventing the wheel, you can check out this simple PHP pagination class:

http://www.catchmyfame.com/2007/07/28/finally-the-simple-pagination-class/

It probably won't be perfect for you out of the box, but pretty easy to change and get back to coding things is much more interesting than pagination :)

+1
source

The only tutorial you need. http://www.php-mysql-tutorial.com/wikis/php-tutorial/paging-using-php.aspx

It worked for me. This tutorial also has Part 2, but you won’t need it. It tells you how to modify existing code.

+1
source

Just get the total amount and refund:

 $result_count = 200; // hypothetical // run your regular code..... $end_pages_to_display = 3; for($i = $result_count; $i <= ($result_count - $end_pages_to_display); $i--) { echo "<a href='index.php?page={$i}'>{$i}</a>"; } 

I mean ... this is not code that will work for your site, but it is accurate logic.

0
source

I'm not sure if your 's' and 'p' $ _GET are for (salt and pepper?), So I just work with "p" for the page.

 <?php $max = '20'; if ($pages > 1) { echo '<br /><p>'; $current_page = ($start/$display) + 1; //add this here... first will always be one echo '<a href="index.php?p=1">First</a>'; if ($current_page != 1) { echo '<a href="index.php?s=' . ($start - $display) . '&p=' . $pages . '">Previous</a> '; } for ($i = 1; $i <= $pages; $i++) { if ($i != $current_page) { echo '<a href="index.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '">' . $i . '</a> '; } else { echo '<span>' . $i . '</span> '; } // add this here... if ( $i == $max){ // stop the for() loop break; // not so fancy way of displaying last two pages, use other example if you want to get fancy. echo '<a href="index.php?p=' . ($pages - 1) . '">'.($pages - 1).'</a> '; echo '<a href="index.php?p=' . ($pages) . '">'.($pages).'</a> '; } } if ($current_page != $pages) { echo '<a href="index.php?s=' . ($start + $display) . '&p=' . $pages . '">Next</a>'; } echo '<a href="index.php?p=1">Last</a>'; echo '</p>'; } ?> 
0
source

Perhaps there will be an easier way: use the pre-created library ...

http://framework.zend.com/manual/en/zend.paginator.html

0
source

Source: https://habr.com/ru/post/1309372/


All Articles