Pagination (Navigation Link) Help
I am attempting to create a better pagination routine for my news management program. The one I'm using now I created back when I was first learning PHP, so I think I can do it better now.
My current one is 120 lines of code, and this new one I'm nearly finished with is only 45 so far. So at the least, I'm saving some space.
What I want to do is display a maximum of five page links at a time, not including previous or next links.
If there are not five page links, display only what is there. Keep the current page in the middle.
Examples:
Pages [8] << 2 3 4 5 6 >>
Pages [8] 1 2 3 4 5 >>
Pages [8] << 4 5 6 7 8
Pages [3] 1 2 3
Pages [5] 1 2 3 4 5
I have this working perfectly (haven't added the << >> buttons, but that'll be easy), but there's a bit of code I still think I can simplify.
If anyone has a minute to look at it and let me know if you know of a better method, please let me know.
Really all I'm looking for is a way to simplify these lines, and possibly shorten up the code even more.
Here is a demo of the script: http://www.vbshelf.com/development/pagination/index.php
Here is the source code: http://www.vbshelf.com/development/p...eatelinks.phps
The links I'm wondering if I can shorten or merge somehow are:
Code:
// Figure out which page to start our links on:
$loopStart = ($currentPage - 2 > 0) ? ($currentPage - 2) : 1;
// Figure out which page finishes our links:
$loopEnd = ($currentPage + 2 > $totalPages) ? $totalPages : ($currentPage + 2);
// If we don't have two pages after the current page, add the difference to the beginning:
$loopStart -= ($loopEnd - $currentPage < 2) ? (2 - ($loopEnd - $currentPage)) : 0;
// If we don't have two pages before the current page, add the difference to the end:
$loopEnd += ($currentPage - $loopStart < 2) ? (2 - ($currentPage - $loopStart)) : 0;
Pretty much the bulk of the function.
Any ideas or comments, let me know.