|
-
Jul 26th, 2004, 06:46 PM
#1
Thread Starter
Stuck in the 80s
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.
-
Jul 26th, 2004, 06:57 PM
#2
Thread Starter
Stuck in the 80s
Update: I added the 'first page' (<<) and 'last page' (>>) links. So the code can be considered complete, now.
Any ideas on how to optimize it or make it better, let me know.
-
Jul 28th, 2004, 06:05 PM
#3
I've had a go at shortening the bit you wnated. Not sure whether you would consider it optimized of not - I'm not sure twhether it is quicker using if statements over the ? : operator.
PHP Code:
<?php
function createlinks($totalItems, $perPage, $currentPage, $link) {
// First, figure out the number of pages there will be total:
$totalPages = floor($totalItems / $perPage);
$totalPages += ($totalItems % $perPage != 0) ? 1 : 0;
// Start building page links:
if ($totalPages > 1) {
$output = 'Pages [' . $totalPages . '] ';
$loopStart = 1; $loopEnd = $totalPages;
if ($totalPages > 5) {
if ($currentPage <= 3) {
$loopStart = 1;
$loopEnd = 5;
} else if ($currentPage >= $totalPages - 2) {
$loopStart = $totalPages - 4;
$loopEnd = $totalPages;
} else {
$loopStart = $currentPage - 2;
$loopEnd = $currentPage + 2;
}
}
// Add the "first page" link:
if ($loopStart != 1) {
$output .= '<a href="' . $link . '1">«</a> ';
}
// Use the variables we setup above to loop the links:
for ($i = $loopStart; $i <= $loopEnd; $i++) {
// If this is our current page:
if ($i == $currentPage) {
// Output bold without a link:
$output .= '<b>' . $i . '</b> ';
// This is not our current page:
} else {
// Output the link:
$output .= '<a href="' . $link . $i . '">' . $i . '</a> ';
}
}
// Add the "last page" link:
if ($loopEnd != $totalPages) {
$output .= '<a href="' . $link . $totalPages .'">»</a> ';
}
return $output;
} else {
return '';
}
}
?>
EDIT: Silly me missed an IF statement out. But you get the idea 
EDIT2: See here for a demonstration on how to use this function.
Last edited by visualAd; Apr 21st, 2005 at 08:35 AM.
-
Aug 1st, 2004, 02:07 PM
#4
Thread Starter
Stuck in the 80s
That looks much cleaner than what I had, and I'd rather have clean code over shorter code. I haven't tried it yet, but as soon as I get the chance, I'll test it and make sure it works, but I'm sure you already did.
I'm not sure about speed differences between if and ? : either. I was just using ? : because I've gained some kind of fondness for them. It looks cooler to me, I guess.
Thanks for the help. I like the way that looks much better than mine.
-
Aug 1st, 2004, 04:08 PM
#5
I tested the code on my server and it appeared to have the same results as yours. The way I usually work out which is more efficient is by counting the number of lines executed.
Your method is always 8 lines where as mine is a minimum of 1 and a maximum of 6. However, the ? : operator is probably faster than using if becuase it doesn't need to check of "else if". But I'm not sure.
Either way the difference is proably negligable and not worth worrying about. I usually just go with which ever looks nicer 
P.S: If you want to confuse newbies, keep the ?'s
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|