...

View Full Version : Pagination with max 10 pages



htcilt
02-05-2010, 01:35 PM
Hi all,

I'm trying to get some pagination to my results page.
I would like the navigation links to only show 5 pages on either side of the current page.

For example if I have 40 pages in total:

Page 1 shows:
1 2 3 4 5 6 7 8 9 10 [Last] Next

Page 18 shows:
[First] 14 15 16 17 18 19 20 21 22 23 [Last]

Currently I have all the page links (1-40 echoed) by using this tutorial:
http://www.phpbuilder.com/tips/item.php?id=2

I'm using Oracle, but if anyone can point me to a mySQL example of the above, I'm sure I can replicate it in Oracle/PHP.

Hope someone can help as I'm completed stumped!

abduraooft
02-05-2010, 01:45 PM
There's a good snippet posted by Fou-Lu at http://codingforums.com/showthread.php?t=151398

htcilt
02-05-2010, 02:11 PM
Thanks for the reply.
Jeepers! thats some fancy code. No idea if I'll ever be able to implement that as I've only been doing PHP for a couple of weeks lol

Len Whistler
02-05-2010, 06:38 PM
You have to add some if statements - or modify the curent ones - to the code below.



// Now loop through the pages to create numbered links
// ex. 1 2 3 4 5 NEXT >>
for ($i=1;$i<=$pages;$i++) {
// Check if on current page
if (($offset/$limit) == ($i-1)) {
// $i is equal to current page, so don't display a link
echo "$i &nbsp; ";
} else {
// $i is NOT the current page, so display a link to page $i
$newoffset=$limit*($i-1);
echo "<a href=\"$PHP_SELF?offset=$newoffset\">$i</a> &nbsp; \n";
}
}


Below is just an example and may have math and syntax errors, but should give you an idea where to start. You will have to come up with something for the last, first and next links, but since you know the current page and how many pages there will be that should be easy.

The current page is ( $i ) and you can use some math to display links that fall within a range.


$min = $i - 5;
$max = $i + 5;

if ($i >= $min) && ($i <= $max) {
echo links
}


-----------

JAY6390
02-05-2010, 06:46 PM
You can use my pagination class (in my signature). There's an example on how to use it on my site along with the source, and shouldn't be too hard to convert to oracle. if you get stuck in any way let me know :)

JohnDubya
02-05-2010, 07:14 PM
You can use my pagination class (in my signature). There's an example on how to use it on my site along with the source, and shouldn't be too hard to convert to oracle. if you get stuck in any way let me know :)

I'll vouch for Jay's pagination code, it's awesome! I got it just to see if I could integrate it into a project, and it worked awesome. Sadly, it would have taken me too much time to integrate it across all the needed pages, so I had to scrap using it, but the script is incredibly well-written and easy to use/customize. Well done, Jay.

JAY6390
02-05-2010, 07:15 PM
Thanks JohnDubya! :)

htcilt
02-06-2010, 11:03 AM
You can use my pagination class (in my signature). There's an example on how to use it on my site along with the source, and shouldn't be too hard to convert to oracle. if you get stuck in any way let me know :)

Thanks Jay :)
I'm new to PHP so I'm not sure where to start with changing your class to work with Oracle. I know Oracle doesn't do LIMIT or mysql functions like mysql_num_rows. But thats about the extent of my knowledge :o

JAY6390
02-06-2010, 11:24 AM
I've not used oracle with PHP before, but there are alternatives to the limit clause
http://www.oracle.com/technology/oramag/oracle/06-sep/o56asktom.html
It's pretty complex by the looks of it

htcilt
02-06-2010, 09:35 PM
Yes Oracle does have me cursing at time lol.
I might persevere with the original code and Len's suggestions, as its partly working at least ;)

I'll post back with code if I figure it out...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum