PaginateIt - A PHP Pagination Class
After coding pagination functionality into a million different sites and wasting too much time every time, I decided to give OOP a shot. I’ve used this class in one site already, but haven’t tested it extensively, so there may still be some bugs.
I also used this opportunity to check out different software licenses and settled on The MIT License as it provided more flexibility. I checked out the GPL that I’ve heard so much about, but I thought it to be too restrictive for this project. This is being provided as is for whatever use anyone may want it, commercial or otherwise, under the condition that my copyright stays intact. I’d like a little credit
I had inline documents within the class, but I forgot to save and I don’t feel like going back and writing them again at the moment. So if you want to know what something does, just ask me. The Readme documents the methods and usage.
Go ahead and leave feedback and comments about anything (licenses, documentation, coding style/preferences, etc). I’d especially like to know if anyone finds this useful. If you need any support, don’t be afraid to ask.
PaginateIt.zip - contains readme and source file
Example - A working example of PaginateIt’s output
PaginateIt.phps - Source
Readme.doc - Documentation, usage, and licensing information
on July 9th, 2005 at 3:19 pm
[…] pretty neat pagination script for anyone to use on their website or blog. Check it out at PaginateIt. I know firsthand that he’s put a lot of work into it, but I told him he s […]
on July 13th, 2005 at 9:09 am
cheers for this. i was in a terrible hurry (as always) to get some quick, modern pagination into a site, and this completely came through.
on July 13th, 2005 at 4:20 pm
Awesome, I’m glad you found it useful. Was there anything you didn’t like about it, or you thought that it could use? Did the readme cover everything pretty good?
I’ll prolly add different tidbits and scripts like this every once in awhile when I need to write one, so check back every now and then and thanks for the feedback.
on July 15th, 2005 at 8:35 pm
yeah the readme was fine by and large. i had it going in a few minutes.
the thing i really liked though was that it’s abstracted from the actual request : most pagination makes you structure your query around it, while this offered the lovely lil limit limit clause you could pop right in your query.
one thing of note, i noticed your back and next links jump to the next “group” of pages, rather than just a single page. for the default behavior, it might be good to have those links either say something like “next 10″ (or whatever).
trivial, trivial usability detail really, and since you can change it without even altering the class anyway i wasn’t particularly fussed either way.
{bookmarked} :]
on July 15th, 2005 at 8:38 pm
oh, incidently it seems to do fine on a site with over 5,000 products, so that’s probably a reasonable stress test, heh.
on July 21st, 2005 at 9:56 pm
Thank you for the wonderful script. I have a few comments:
1. I added this function because I think it lacks your get functions:
function GetStartIndex(){
return ($this->currentPage * $this->itemsPerPage - $this->itemsPerPage);
}
2. I think there is a bug on your script. I have these:
$PaginateIt =& new PaginateIt();
$PaginateIt->SetItemsPerPage(5);
$PaginateIt->SetItemCount(19);
$PaginateIt->SetLinksFormat( ‘>’ );
echo $PaginateIt->GetPageLinks();
It then displays: 1 | 2 | 3 | 4
But when I try to click the “4″ link, it displays: 0 | 1 | 2 | 3 | 4
on July 22nd, 2005 at 11:57 pm
Cool, thanks for the feedback Adam. The SQL limit is what was most useful for me, so I included it. As for jumping through groups of links, I don’t particularly like it, but it seems like it’s the most practical approach. You’re right about the usability issue, though, I was just looking for a good way to word it.
Yep, that’s a bug, Jen. Thanks for pointing it out. I believe it’s fixed now and hopefully there’s not anymore. But I got to upgrade my version to 1.0.1, sweeeettt =P I’ll give you credit if I ever add a changelog or somethin. I thought about adding the Start Index, but didn’t find it useful for me, although I shoulda known someone else could have used it. I’ll add it when I find more time. Thanks for the feedback, and I’m glad you found it useful.
on July 23rd, 2005 at 8:54 pm
Wow, you are a total resource now! That is awesome!
on July 26th, 2005 at 10:52 am
Brady:
I have to tell you - this script was pretty easy to use!
I’d been using a different paging class that caused some issues with more advanced SQL statements. I didn’t have the time to debug the paging class myself, so I turned to Google and found your script. After looking at the code and the API, I managed to implement it in about 15 minutes.
Excellent work.
Adam Gedde
on August 7th, 2005 at 2:09 pm
upgraded. still waitin for a new post to read
on August 8th, 2005 at 9:45 am
sorry, but I don’t speak english well.
Is it error here?
« Back 1… 2 3 4 5 6 …100 Next »
on August 8th, 2005 at 9:47 am
aaa
I’m add new method
function GetLimit(){
return ‘ LIMIT ‘.($this->currentPage-1)*$this->itemsPerPage.’, ‘.$this->itemsPerPage;
}
for query to MySQL
on August 9th, 2005 at 8:34 am
upsss soryy my mistake
on August 12th, 2005 at 12:12 am
odd, for some reason i’m getting a potentially weird result with the new version while trying to impliment it into my homebrew php bugtracking system. it might just be my calling class functions in an inproper order perhaps…
//snip
$paginate = new PaginateIt(); // prep pagination
$paginate->SetItemsPerPage($limit);
$queryLIMIT = $paginate->GetSqlLimit();
$paginate->SetQueryString("$s_PersistentVars");
$connection->queryString("SELECT n_ID FROM $table $WHEREClause");
$connection->getRows($connection->result);
$numberOfRows = $connection->numrows;
$paginate->SetItemCount($numberOfRows);
$paginate->SetLinksFormat( 'Prev 10', ' | ', 'Next 10' );
$pages = $paginate->GetPageCount();
//snip
it’s pseudo hungarian notation just without the mneumonic, so you probably get the idea.
a few sample results of the output:
http://www.graphicsdesign.org/paginate1.gif
the first one prints properly as far as the numeric links and the separation character go, but the skip links don’t seem to show. i’m assuming this is because there’s less than 10 pages in the results.
http://www.graphicsdesign.org/paginate2.gif
but the skip links seem to return when there’s 0 results, so i’m not sure…
is it maybe just a simple case of it not checking if there’s 0 results?
on August 12th, 2005 at 4:06 am
Great and simple use class, even though for a begginer like me! You made my day thanks a lot!
on August 13th, 2005 at 5:35 pm
Thanks for all the comments and feedback everyone.
I looked into the functionality, adam, and I designed it that way for some reason, although I admit it’s kinda weird why I did it. I updated the script so that the next and back links won’t show up when there’s 0 results.
on August 14th, 2005 at 2:59 pm
*starts chanting*
Update, update!!
on August 16th, 2005 at 12:19 pm
I got this working but not working right. Is there an actual “working” version of this class pulling data from a database?
Thx!
on September 5th, 2005 at 3:11 am
Could anyone help a newbie and point me to a code example how to get this to work on a flatfile (CSV). I use fgetcsv to return the data. Thanks very much in advance!!
on September 8th, 2005 at 6:27 am
your script generated bad links
look
3
should be
3
thank’s a lot
on September 8th, 2005 at 6:28 am
look
index.php?nav=branza&id=1&page=3
should be
index.php?nav=branza&id=1&page=3
on September 8th, 2005 at 7:03 am
Could you help me out a little, adm? I’m not seeing a difference between those two.
on September 8th, 2005 at 10:47 am
it’s not my faul, it’s faul scritp guest book on this site.
look
‘index.php?nav=branza & id=1 & a m p ; page=3′
should be
‘index.php?nav=branza& a m p ; id=1 & a m p ; page=3′
on September 8th, 2005 at 5:45 pm
Oh, I think I gotcha, you mean it’s invalid XHTML. Well that’s no bueno, so with that I announce the release of version 1.0.3. Ohhhh, ahhhhh, heh, ok, enough of that. I fixed it up so that it spits out valid XHTML links now. Thanks for bringing that to my attention adm and sorry about the formatting problems, Wordpress does that automatically.
on September 11th, 2005 at 3:37 pm
Thank’s You. I’m sorry for my language;)
I try to be better. Good luck!
pleas remove my previous comment;)
on October 27th, 2005 at 10:11 am
Brady,
This is a great little script. Would you be able to point me in the right direction with using this with an array rather than a database?
Thanks!
on October 30th, 2005 at 7:42 am
Hello,
First I would like to say, that you have done great job! This class is fantastic.
But newby as I am, I have some problems. It works fine in my server as it is, but I just can’t get it to work inside my system. I want to use this script in a page ‘?id=something’ not just ‘index.php’. Can you please give a short script, how to get it work then.
I am just getting echod nr “1″, that all, no page links, nothing
I am using:
$PaginateIt = new PaginateIt();
$PaginateIt->SetItemsPerPage(5);
$PaginateIt->SetQueryStringVar(’lk’); // page = lk, like ?id=1&lk=23
$PaginateIt->SetLinksHref(’index.php’);
$PaginateIt->SetLinksToDisplay(8);
$total_res = mysql_num_rows($base->query(”SELECT COUNT(*) FROM table”));
$PaginateIt->SetItemCount($total_res);
$p = $base->query(”SELECT * FROM table ORDER BY id DESC” . $PaginateIt->GetSqlLimit());
$PaginateIt->SetLinksFormat( ‘’, ‘ ‘, ‘’ );
echo $PaginateIt->GetPageLinks();
Thanks for any help…
on October 30th, 2005 at 7:46 am
Note:
$PaginateIt->SetLinksFormat( ‘image1’, ‘spacer‘, ‘image2’ ); - it had HTML code images inside, but your guestbook deleted them
on October 31st, 2005 at 11:48 pm
I updated the class so that it now supports arrays. Just pass an array like this:
$newArray = $PaginateIt->GetCurrentCollection($array);This will return an array containing only the elements that should be displayed on the current page.
on November 16th, 2005 at 10:16 pm
For Postgres:
function GetSqlLimit(){ return ‘ OFFSET ‘.($this->currentPage * $this->itemsPerPage - $this->itemsPerPage).’ LIMIT ‘.$this->itemsPerPage;
on November 17th, 2005 at 9:10 am
Thanks for the PaginateIt class! Very useful! I hacked the class a little so that the NEXT/PREV jumps to the next and previous pages respectively, while the arrows jump to the next sets. Other than that, I didn’t have to change a thing. Very cool class that saved me a ton of time! Thanks again!
on November 23rd, 2005 at 11:24 am
look:
http://www.zundam.net/paginate_wrong.gif
something wrong?
???
on November 24th, 2005 at 1:31 am
When item count is set to something like 150 it just displays 15 links. Any body know why?
Thanx in advance.
on November 24th, 2005 at 1:43 am
adm: I don’t see anything wrong with that. Could you provide more details?
ruvan: You can set the number of links to display using the SetLinksToDisplay() method.
on November 24th, 2005 at 10:22 am
$PaginateIt = new PaginateIt();
$PaginateIt->SetItemCount(150);
echo $PaginateIt->GetPageLinks();
this just displays 15 links. i mean i could only browse just 15 links(not per page. All)
on November 24th, 2005 at 11:54 am
1… 2 3 4 5 |6| 7 8 9 …20
should by
1 2 3 4 5 |6| 7 8 9 …20
on November 26th, 2005 at 7:30 pm
Thanks so much for a great piece of work. With a few modifications, it does exactly what I wanted and saved me a lot of work.
on December 3rd, 2005 at 2:29 pm
Hi, I have this script that checks a mailbox but don t know how to paginate the results using the PaginateIt class, help me please. Thanks
The script:
$mbox = imap_open(”{localhost:143}”, “username”, “password”)
or die(”can’t connect: ” . imap_last_error());
$MC=imap_check($mbox);
$MN=$MC->Nmsgs;
$overview=imap_fetch_overview($mbox,”1:$MN”,0);
if (is_array($overview)) {
foreach ($overview as $val) {
echo “$val->msgno - $val->date - $val->subject\n “;
}
}
imap_close($mbox);
on December 6th, 2005 at 12:57 pm
look
http://www.krakow.targi.eco.pl/index.php?nav=opcja&id=7&page=7
Is it wrong or normal situation?
1… | 2 | 3 | 4 | 5
on December 6th, 2005 at 1:24 pm
That’s actually the correct functionality, admx, but I can see how it might be confusing. It shouldn’t be too hard to change, but it’s not necesarily wrong, so I’ll leave it be for now.
For anyone interested, here’s the reply I sent to Henker. I haven’t tested it, but this is how it should work theoretically.
<?php
include(‘PaginateIt.php’); // set this to the location of the PaginateIt class
$mbox = imap_open(”{localhost:143}”, “username”, “password”) or die(”can’t connect: ” . imap_last_error());
$MC=imap_check($mbox);
$MN=$MC->Nmsgs;
$overview=imap_fetch_overview($mbox,”1:$MN”,0);
if (is_array($overview)) {
$PaginateIt->SetItemsPerPage(10); // change this to the number of items you want to show
$PaginateIt->SetItemCount(count($overview));
$overview = $PaginateIt->GetCurrentCollection($overview));
foreach ($overview as $val) {
echo “$val->msgno - $val->date - $val->subject\n “;
}
}
imap_close($mbox);
echo $PaginateIt->GetPageLinks();
?>
on December 7th, 2005 at 7:57 pm
Hi Brody,
i have been trying to use your pagination script to do ldap result search, any help/sample scripts will be greatly appreciated.
cheers…
on January 9th, 2006 at 6:37 pm
Brody,
Can you provide an example for pagination a MySQL result set? Do I need 2 queryies? Here is what I have now.
$PaginateIt = new PaginateIt();
$PaginateIt->SetItemsPerPage(25);
$categories = mysql_query(”SELECT id FROM table WHERE id = ‘$id’”);
$PaginateIt->SetItemCount(mysql_num_rows($categories));
$categories = mysql_query(”SELECT id FROM table WHERE id = ‘$id’” . $PaginateIt->GetSqlLimit());
Then I print my results here…
echo $PaginateIt->GetPageLinks();
Any help is appreciated!
on January 12th, 2006 at 11:17 am
Hey Brady,
This class is saving me tons of work! However, I’m having an issue with a customization I’m working on. I created a select box that allows users to change the # of records per page, it works great with the current page they are on, but the problem arises when they click one of the page links, i.e. page 2.
When they click the link, it resets the data grid back to the original value of “SetItemsPerPage()”. For example, say it is set to 10 by default. When they change it, the records update from only 10 on the page to what they choose and the list and the number of page links adjusts accordingly. If they want to go to page 2, when they click page 2 it resets the data grid back to 10 records per page.
Is this something that can be manipulated within the class to read the value of SetItemsPerPage() for each page in the set, or do I need to take another approach?
Here’s a brief excerpt of my code:
$PaginateIt =& new PaginateIt();
if(isset($_POST[’calls’])){
$PaginateIt->SetItemsPerPage($_POST[’calls’]);
}else{
$PaginateIt->SetItemsPerPage(10);
}
$PaginateIt->SetLinksToDisplay(3);
$items = mysql_query(”…sql statement here…”, $db);
$tItems = mysql_num_rows($items);
$PaginateIt->SetItemCount($tItems);
$PaginateIt->SetLinksFormat( ‘>’ );
Thanks!
–kg
on January 24th, 2006 at 11:38 am
[…] y: archie
So I’m now using an RSS parsing class and a PHP Pagination class to make the galleries work. The more I try to get working, the more th […]
on January 30th, 2006 at 4:27 pm
// Example Working
$PaginateIt = new PaginateIt();
$PaginateIt->SetItemsPerPage(1);
$PaginateIt->SetLinksToDisplay(5);
// first query, see how many results are actually in the table
$query = mysql_query(”SELECT * FROM table”);
$result = mysql_num_rows($query);
$PaginateIt->SetItemCount($result);
// second query, find only the relevant results
$sql = “SELECT * FROM table ” . $PaginateIt->GetSqlLimit();
$query = mysql_query($sql);
// loop through second query
while($row = mysql_fetch_array($query))
{
echo “” . $row[’title’] . “” ;
}
$PaginateIt->SetLinksFormat(” ” );
echo $PaginateIt->GetPageLinks();
on February 10th, 2006 at 4:46 am
[…] tats d’un moteur de recherche. Après quelques recherches Google, j’ai trouvé PaginateIt qui me convient parfaitement. Cette petite librairie fournit une classe bien do […]
on February 16th, 2006 at 4:19 pm
[…]
Something Metaphorical » PaginateIt - A PHP Pagination Class Something Metaphorical » PaginateIt - A PHP Pagination Class This […]
on February 19th, 2006 at 2:18 pm
Congrats for this very usefull class: it took me 15mn to integrate it in a working page with awfully implemented pagination.
Lucky me that thought, at last, searching google before reinventing the wheel it:)
Cheers!
on March 20th, 2006 at 12:32 am
I have problem in passing the query string from page 1 to the next page.
My query string consist of dynamic “where” condition, and the values are passed from “search feature”.
The PaginateIt itself works very well, but as I try to access the next page, the “where” value turned back to null.
Can anybody help me? Thanks a lot.
Code:
List Rajut
0 AND statusDEL=’1′”;
if ($tahun_from!=”"){
if($bulan_from!=”"){
if($tanggal_from!=”"){
if($tahun_until!=”"){
if($bulan_until!=”"){
if($tanggal_until!=”"){
$date_from=$tahun_from.$bulan_from.$tanggal_from;
$date_until=$tahun_until.$bulan_until.$tanggal_until;
if($date_untilSetQueryString($query);
$rajuts=mysql_query($query);
if (!$rajuts){
echo(”");
echo(”Error retrieving rajut’s production data from database”.
“Error:”.mysql_error());
exit();
}
$result = mysql_num_rows($rajuts);
$PaginateIt->SetItemCount($result);
echo $PaginateIt->GetPageLinks();
$rajuts=mysql_query($query.$PaginateIt->GetSqlLimit());
while($rajut=mysql_fetch_array($rajuts)){
……}
?>
on March 20th, 2006 at 12:36 am
I think my code was not complete uploaded
But btw. can anyone give me example how to pass query string generated from a “search feature” so that every pages displayed will have same query string.
Thx
on March 21st, 2006 at 9:51 pm
Hello Sen,
The script should automatically pick up the querystring if it’s present when the PaginateIt class is called. Otherwise, you can use the SetQueryString method to add variables to the querystring before you call GetPageLinks.
Example:
$PaginateIt->SetQueryString(’?search=value&var2=value2&etc=more’);
Let me know if that answers your question.
on March 22nd, 2006 at 6:21 am
Hallo brady,
thanks, it could solve the problem.
But I got another probem now
I made a PHP web site with search function:
$select = “SELECT *”;
$from = ” FROM data_one”;
$where = ” WHERE ID>0″;
//here comes the next condition inserted via the form and my problem starts here:
if($value!=”"){
$where .= “AND constraint LIKE ‘%$value%’”;
}
$query = $select $from $where;
$temps = mysql_query($query);
while($t = mysql_fetch_array($temps)){
…. //code
}
This page works fine combined with the PaginateIt() but as I enter number into the constraint, such as 5000100… the querystring was only correct at the first page.
I try to echo the querystring at the other page… and the result, it changed from LIKE ‘%5000100′% into LIKE P00100%’.
Why? Any Idea?
My second problem still realted with the form’s value: I try to Set the querystring using the method you gave, but you see my code… I must set the dynamic “where clause” inside my code, and if I try this
$PaginateIt->SetQueryString(’?where=$where’);
and I retrieve the value by using GET :
$where=$_GET[’where’];
it always returns the empty form, because the form’s value becomes empty at the next page. I think, because It is always re-set by the SetQueryString method before I can use the where value I need (which I set at the first page).
Maybe you can suggest any solution?
Thanks a lot Brady
Sen
on March 22nd, 2006 at 10:10 pm
Hmm, I can’t tell what your problem is from that code, but here’s a few suggestions.
You should never pass anything through the querystring that you’re going to use in a SQL statement, it’s a major security flaw. Someone could easily drop your database if you use that method. Also, all data from the query string should be checked and verified before you use it in your code.
I simplified the code a little. I’m assuming you have a page with a search form and when someone clicks ‘Search’, it goes to the search results. If that’s the case, here’s how you should set the querystring.
if(!isset($_GET[’value’])){
$PaginateIt->SetQueryString(’value=whatever’);
}
// if ‘value’ is set in the querystring, convert applicable characters to htmlentities; this is for security
$value = (isset($_GET[’value’])) ? htmlentities($_GET[’value’]) : ‘’;
$sql = ‘SELECT * FROM data_one WHERE ID>0′;
if(!empty($value)){
$sql .= ” AND constraint LIKE ‘%$value%’”;
}
$query = mysql_query($sql);
while($t = mysql_fetch_array($query)){
// code
}
I hope that helps.
on March 31st, 2006 at 9:52 am
Hi Brady,
Great class man!
I have implemented it as a postnuke module, purely as an example for people to integrate the class with their postnuke modules.
Download here:
http://www.postnuke.co.nz/downloads/paginate.zip
Once again thanks for your work.
-Lobos
on April 2nd, 2006 at 6:03 am
Hi Brady,
I found new error
http://zundam.net/pagination_error1.png
on April 14th, 2006 at 3:26 pm
I’ve set $PaginateIt->SetLinksToDisplay(10);
First display is 1-2-3-4-5-6-7-8
If I go to page 8 pages # links are “-1, 0,1,….8″
The same is when I go to page 7.
It seems like the class forces to display 10 numbers.
on April 14th, 2006 at 5:39 pm
Thanks for the comments, Lobo, I’m glad you found it useful.
adm and Daniel, I believe you’re both experiencing the same bug. I noticed the bug awhile ago, but didn’t have time to update. I went ahead and released an updated version, so using that should fix ya’lls problem.
on April 28th, 2006 at 5:57 pm
Big B-
I am having a bit of a problem getting the class to read my array (a completed search result set). Do you happen to have a walk-through or a code example for a newbie such as myself, that could show me how to get my array read?
danke
on May 3rd, 2006 at 6:52 am
Sorry about the slow response, Sabin. I’ve been away on vacation. Here’s how you’d do what you want.
$array = range(5, 69); // some array; your search result set
$PaginateIt =& new PaginateIt();
$PaginateIt->SetItemCount(count($array));
$pageArray = $PaginateIt->GetCurrentCollection($array));
/*
do whatever you want to display $pageArray
*/
echo $PaginateIt->GetPageLinks();
That should be all there is to it. You can set any other variables before you call the “GetCurrentCollection” method. Let me know if that works for you.
on May 21st, 2006 at 2:27 pm
Brady, I just wanted to say thanks so much for the great script. This surely saves me a great deal of time!
on May 25th, 2006 at 6:27 am
i’m confused on how this works with a sql query.
if someone can illuminate for me — i’ve got a page with about 5000 items that need paginated.
on July 18th, 2006 at 11:02 am
This is a real nice peace of code , a saves a lot of work for me, thanks a lot
on July 25th, 2006 at 11:50 am
Wicked class
thanks a bunch
on August 1st, 2006 at 4:10 pm
It would be great if I someone could post their source code of a very basic pagination page and/or function.
I am having trouble in that some pages do not ‘page’ when clicking on the 1, 2, 3, etc.
on August 7th, 2006 at 9:54 am
How could I make this work with a dynamically generated SQL statement?
on August 15th, 2006 at 10:09 pm
Hi, thanks alot for this, saved me hours
on August 16th, 2006 at 10:59 am
still wrong
-1 0 1 2 3 4 5 6 7 8
on August 16th, 2006 at 1:11 pm
I don’t know how you’re getting that output adm. Could you send me exactly what you’re doing?
on August 16th, 2006 at 1:21 pm
include(’paginate.class.php’);
$PaginateIt = new PaginateIt();
$PaginateIt->SetItemsPerPage(30);
$PaginateIt->SetLinksToDisplay(10);
$PaginateIt->SetItemCount($data[’count’]);
$PaginateIt->SetLinksFormat(’«’,’ ‘,’»’);
$limit = $PaginateIt->GetSqlLimit();
on August 16th, 2006 at 10:25 pm
Oops, I figured out the problem. The zip file contained the fix, but the .phps source file didn’t have the fix implemented. Sorry about that, everything should be working now.
on August 17th, 2006 at 2:31 pm
good job:)
my sugestion:
change
$this->SetQueryString($_SERVER[’QUERY_STRING’]);
for
if(isset($_SERVER[’QUERY_STRING’])){
$this->SetQueryString($_SERVER[’QUERY_STRING’]);
}else{
$this->SetQueryString();
}
on August 26th, 2006 at 2:05 pm
First off, newbie shewbie, I’m a fucken retard.
I am attracted to this script because I assume it takes just a little tinkering to get it to work. My setup looks like this and I get all the nice links at the bottom of the page. My problem is that I change the number SetItemsPerPage number and it continues to show all the items from the column/table regardless. The number of items per page doesn’t change.
?>
SetItemsPerPage(5);
$PaginateIt->SetItemCount(100);
echo $PaginateIt->GetPageLinks();
?>
on August 28th, 2006 at 8:45 pm
thx. got it to go after a bit in 21 lines of code. I wasn’t looking forward to doing this myself, so thanks again. I also had a question about outputting an array slice using the currentPage and itemsPerPage vars, so I wouldn’t have to make another sql query. With an associative array, that was too hard on my tired brain. Instead I opted for doing another sql query with the GetSqlLimit() function.
on August 30th, 2006 at 11:05 am
[…] clase en PHP para paginar registros de forma sencilla y eficiente. Aqui No Comments to “Paginación en PHP& […]
on August 31st, 2006 at 7:15 am
Great script… Iglad I found this an appriciate the work!
I’m using codeigniter as ap-framework and uses searchengine friendly links
like
www.mysite.net/products/results/1
where
-> www.mysite.net/products/ is the pathinfo (no index.php)
-> results the queryParameter
-> 1 is currentPageNumber
How I configurte PagniateIt in this environment especially how to setup the queryStr?
on September 1st, 2006 at 7:16 am
Brady,
I’ve been using this great class for 2 years now and it has saved me a great amount of work.
Thank you.
You really do great work.
on September 22nd, 2006 at 9:16 am
Just started using and it works really well. One problem I’m having is in my understanding…probably because my php is very poor.
No problems at all on a standard grid, but when I apply to a search page I get problems. If i use SetQueryString hardcoded, e.g (’field2=123&field2=abc), absolutely no problem, the page paginates properly with the expected search results. Obviously, I need this to be driven by variables as input from the original search form. But as soon as try to apply a variable e.g (’field1=$xxx&field2=$yyy) I get errors as the sql query seems to fail.
I gues I’m doing something really stupid. Does anyone know what ?
on October 28th, 2006 at 5:09 am
first of all thank you for a great class!! good work
i was just having trouble with the following:
can anybody help me change the links so that when i click next/back,
only the page next to it should be displayed. because it jumps to the page according tho the linkstodisplay..
thanks..
on January 3rd, 2007 at 8:05 pm
I had tried
$PaginateIt->SetQueryString(’cPub=cariDonatur&cOpt=hasilCariDonatur&operator1=$operator1&query=$query1′);
But the result for operator1 = $operator1 and query = $query1
By the way, here is my full code, Please tell me the lines wrong below.
//My Full Code
include(’inc/PaginateIt.php’);
$PaginateIt = new PaginateIt();
$PaginateIt->SetItemsPerPage(25);
$PaginateIt->SetLinksToDisplay(15); // view link : nav/ page
// first query, see how many results are actually in the table
$QRY1 = mysql_query(”SELECT * from $tblDonatur where $operator1 like ‘%$query%’ order by nama”);
$result = mysql_num_rows($QRY1);
$PaginateIt->SetItemCount($result);
// second query, find only the relevant results
// $SQL = “SELECT * FROM com_donatur ” . $PaginateIt->GetSqlLimit();
// $QRY2 = mysql_query($SQL);
// loop through second query
$JML=mysql_num_rows($QRY1);
if($JML>0) {
echo “Result of : ‘ $operator1 ‘ , with the key ‘ $query ‘, was $JML data.”;
//echo ”
//
//”;
//get string2 url;
$PaginateIt->SetQueryString(’cPub=cariDonatur&cOpt=hasilCariDonatur&operator1=$operator1&query=$query1′);
echo “Page. “; $PaginateIt->SetLinksFormat(” > “);
echo $PaginateIt->GetPageLinks();
echo “”;
echo ”
ID
Nama
Tempat, Tgl. Lahir
Alamat
Kota
Propinsi
Kodepos
Telp
Status
BSPI“;
echo “”;
while($ROW = mysql_fetch_array($QRY1))
{
if($ROW[blnLahir]==1) { $cvtBln=”Jan”; }
elseif($ROW[blnLahir]==2) { $cvtBln=”Feb”; }
elseif($ROW[blnLahir]==3) { $cvtBln=”Mar”; }
elseif($ROW[blnLahir]==4) { $cvtBln=”Apr”; }
elseif($ROW[blnLahir]==5) { $cvtBln=”Mei”; }
elseif($ROW[blnLahir]==6) { $cvtBln=”Jun”; }
elseif($ROW[blnLahir]==7) { $cvtBln=”Jul”; }
elseif($ROW[blnLahir]==8) { $cvtBln=”Agu”; }
elseif($ROW[blnLahir]==9) { $cvtBln=”Sep”; }
elseif($ROW[blnLahir]==10) { $cvtBln=”Okt”; }
elseif($ROW[blnLahir]==11) { $cvtBln=”Nop”; }
else { $cvtBln=”Des”; }
echo ”
“;
userId($ROW[IDb], $ROW[IDc]);
echo ”
$ROW[nama]
$ROW[tempatLahir], $ROW[tglLahir]-$cvtBln $ROW[thnLahir]
$ROW[alamat]
$ROW[kota]
$ROW[propinsi]
$ROW[kodepos]
$ROW[telp]
$ROW[status]
$ROW[bspi]”;
echo “”;
}
echo “”;
$PaginateIt->SetLinksFormat(” > “);
echo $PaginateIt->GetPageLinks();
}
else
{
echo “Tidak ditemukan Data, berdasarkan Kata Kunci ‘ $query ‘”;
}
on January 3rd, 2007 at 8:07 pm
I mean the result of string operator1 and query is still a variable, not the values. Thank you so much..
on January 3rd, 2007 at 9:06 pm
Hairul, I didn’t look over your code too closely because I noticed the following and thought it might be the cause of your problems.
$PaginateIt->SetQueryString(’cPub=cariDonatur&cOpt=hasilCariDonatur& operator1=$operator1&query=$query1′);
You have single quotes around the parameters instead of double quotes. If you’re going to embed variables inside strings in PHP, you must use double quotes. Anything inside of single quotes doesn’t get parsed by PHP and is read as a string.
$PaginateIt->SetQueryString(“cPub=cariDonatur&cOpt=hasilCariDonatur& operator1=$operator1&query=$query1“);
Let me know if that works for you.
on January 5th, 2007 at 9:16 pm
I really appreciate this class!
I am having a problem with $PaginateIt->GetPageLinks(); (where it actually spits out the linked page numbers). At the moment, no matter how many results it finds it just prints the number ‘1′ there.
If I manually enter a different page variable in my URL it reloads the page with the appropriate rows, but still prints a ‘1′. From these other pages, the ‘1′ is a link and goes to the correct page.
Any ideas?
on February 1st, 2007 at 12:29 pm
Hello,
is it possible to always show the SetLinksFormat e.g. always showing the ‘
on February 1st, 2007 at 12:32 pm
Hello,
is it possible to always show the SetLinksFormat e.g. always showing the “
on February 4th, 2007 at 2:28 pm
Thanks for a great class, however I set it up and this is what I see:
“Fatal error: Cannot redeclare class PaginateIt in C:\XXX\classes\PaginateIt.php on line 2″
Any ideas? Does the class work with PHP5?
David.
on February 6th, 2007 at 5:37 am
What about URL rewriting ??
How can I make this script work with url rewrited pages ?
on February 13th, 2007 at 1:26 pm
Hi, is this script support the use of next, back with the normal behavior ?
Next goes to next page on not 10 pages after ?
Same behavior for Back …
By the way, howesome work
thnaks
on February 18th, 2007 at 6:31 pm
[…] ode examples floating about the net but not so many classes. Anyway here’s the link. http://www.bradyvercher.com/php-pagination-class.html This entry […]
on March 7th, 2007 at 11:54 pm
hello…
I saw your code on the net and I have been trying to insert this on my query code. Can you please help me. I haven’t slept in a couple of days. here is what I have now and I want to display 25 results per page. I would really appreciate your help, Thank you in advance.
$result = mysql_query(”SELECT * FROM listed ORDER BY SCHOOL”);
echo ”
School
Title
Edition
Author(s)
Price
Email
“;
while($row = mysql_fetch_array($result))
{
echo “”;
echo “” . $row[’School’] . “”;
echo “” . $row[’Title’] . “”;
echo “” . $row[’Edition’]. “”;
echo “” . $row[’Author(s)’] . “”;
echo “” . $row[’Price’] . “”;
echo “” . $row[’Email’] . ““;
echo “”;
}
echo “”;
mysql_close($con);
?>
on March 20th, 2007 at 8:29 am
Can help me how to apply Paginateit to this array
Click to see the product';
}
?>
on April 2nd, 2007 at 9:07 pm
Thanks for the great script. Makes my life so much easier.
One question as I can’t for the life of me get the Next and Previous links to show up. Just the page numbers, which work perfectly.
Here’s my code: ($result_array is a multi dimensional array of data from MySQL)
$pagination = new PaginateIt();
$pagination->SetItemCount($rec_count);
$pagination->SetItemsPerPage(10);
$pagination->SetLinksFormat(”BACK”, ‘ | ‘, “NEXT”);
$current_result = $pagination->GetCurrentCollection($result_array);
echo $pagination->GetPageLinks()
foreach($current_result as $result) {
// Display individual record data
}
What am I doing wrong? It seems to happen around line 112 of your class:
if(isset($start)){
if($start > 1){…..
$start is always 1 when I output it
on April 22nd, 2007 at 11:49 am
I had the same issue as Aaron and Andre where the page links always display a ‘1′. The problem is that I copied Aaron’s code that he posted. to make a long explanation short, inside PaginateIt.php, the Brady is initilizing the class already as $PaginateIt = new PaginateIt();
Aaron, remove your call to $pagination = new PaginateIt(); and call member functions like “$PaginateIt->”. Otherwise comment out the last line in PaginateIt.php where the class is initialized and call member functions using your variable like “$pagination->”.
on April 22nd, 2007 at 12:32 pm
Make back button, next button display correctly and eliminate 1… 1 in the page links. Replace GetPageLinks() function with:
function GetPageLinks(){
$strLinks = ‘’;
$pageCount = $this->GetPageCount();
$queryString = $this->GetQueryString();
$linksPad = floor($this->linksToDisplay/2);
if($this->linksToDisplay == -1){
$this->linksToDisplay = $pageCount;
}
if($pageCount == 0){
$strLinks = ‘1′;
}
elseif($this->currentPage - 1 linksToDisplay + 1 == 0) || $this->linksToDisplay > $pageCount){
$start = 1;
}
elseif($pageCount - $this->currentPage linksToDisplay + 1;
}
else {
$start = $this->currentPage - $linksPad;
}
if(isset($start)){
//if($start > 1){
if($this->currentPage > 1){
if(!empty($this->pageJumpBack)){
$pageNum = $start - $this->linksToDisplay + $linksPad;
if($pageNum linksHref.$queryString.$pageNum.’”>’;
$strLinks .= $this->pageJumpBack.'’.$this->pageSeparator;
}
if(($this->currentPage - $linksPad) > 1) {
$strLinks .= ‘linksHref.$queryString.’1″>1…‘.$this->pageSeparator;
}
}
if($start + $this->linksToDisplay > $pageCount){
$end = $pageCount;
}
else {
$end = $start + $this->linksToDisplay - 1;
}
for($i = $start; $i currentPage){
$strLinks .= ‘linksHref.$queryString.($i).’”>’;
$strLinks .= ($i).’‘.$this->pageSeparator;
}
else {
$strLinks .= $i.$this->pageSeparator;
}
}
$strLinks = substr($strLinks, 0, -strlen($this->pageSeparator));
if($start + $this->linksToDisplay - 1 pageSeparator.’linksHref.$queryString.$pageCount.’”>’;
//$strLinks .= ‘…’.$pageCount.’‘.$this->pageSeparator;
$strLinks .= ‘…’.$pageCount.'’;
}
if(!empty($this->pageJumpNext) && $this->currentPage linksToDisplay + $linksPad;
if($pageNum > $pageCount){
$pageNum = $pageCount;
}*/
$pageNum = $this->currentPage + 1;
$strLinks .= $this->pageSeparator . ‘ linksHref.$queryString.$pageNum.’”>’;
$strLinks .= $this->pageJumpNext.’‘;
}
}
return $strLinks;
}
on April 22nd, 2007 at 12:43 pm
Update: Back button still was not going back only one page. Fixed in this code:
function GetPageLinks(){
$strLinks = ‘’;
$pageCount = $this->GetPageCount();
$queryString = $this->GetQueryString();
$linksPad = floor($this->linksToDisplay/2);
if($this->linksToDisplay == -1){
$this->linksToDisplay = $pageCount;
}
if($pageCount == 0){
$strLinks = ‘1′;
}
elseif($this->currentPage - 1 linksToDisplay + 1 == 0) || $this->linksToDisplay > $pageCount){
$start = 1;
}
elseif($pageCount - $this->currentPage linksToDisplay + 1;
}
else {
$start = $this->currentPage - $linksPad;
}
if(isset($start)){
//if($start > 1){
if($this->currentPage > 1){
if(!empty($this->pageJumpBack)){
/* $pageNum = $start - $this->linksToDisplay + $linksPad;
if($pageNum currentPage - 1;
$strLinks .= ‘linksHref.$queryString.$pageNum.’”>’;
$strLinks .= $this->pageJumpBack.’‘.$this->pageSeparator;
}
if(($this->currentPage - $linksPad) > 1) {
$strLinks .= ‘linksHref.$queryString.’1″>1…‘.$this->pageSeparator;
}
}
if($start + $this->linksToDisplay > $pageCount){
$end = $pageCount;
}
else {
$end = $start + $this->linksToDisplay - 1;
}
for($i = $start; $i currentPage){
$strLinks .= ‘linksHref.$queryString.($i).’”>’;
$strLinks .= ($i).’‘.$this->pageSeparator;
}
else {
$strLinks .= $i.$this->pageSeparator;
}
}
$strLinks = substr($strLinks, 0, -strlen($this->pageSeparator));
if($start + $this->linksToDisplay - 1 pageSeparator.’linksHref.$queryString.$pageCount.’”>’;
//$strLinks .= ‘…’.$pageCount.’‘.$this->pageSeparator;
$strLinks .= ‘…’.$pageCount.'’;
}
if(!empty($this->pageJumpNext) && $this->currentPage linksToDisplay + $linksPad;
if($pageNum > $pageCount){
$pageNum = $pageCount;
}*/
$pageNum = $this->currentPage + 1;
$strLinks .= $this->pageSeparator . ‘ linksHref.$queryString.$pageNum.’”>’;
$strLinks .= $this->pageJumpNext.’‘;
}
}
return $strLinks;
}
on April 22nd, 2007 at 2:01 pm
Thanks, Nick. Was there anything that needs fixing in the original script, or are these just changes to suit personal preferences? I realize that the functionality is kinda weird, so I was working on a second release to this that offered different methods of pagination, but I never got around to releasing it. I believe everything works perfectly in the new version, but there are things I’d like to change. Let me know if you’d like a copy of it and I’ll send it your way.
on July 4th, 2007 at 10:50 am
[…] Paginate database results
Pagination: Easy as PREV 1 2 3 NEXT
PHP Pagination with MySQL
PaginateIt - A PHP Pagination Class
[…]
on August 2nd, 2007 at 11:39 am
I just recently found your paginate class. It works great, but I have one question. I set it up with 10 links per page, 10 items per page, and there are 390 pages. If I am on page 1 and click the NEXT button, it jumps to page 16. From there on, it goes from 16 to 26 to 36 etc, but when you are on the first group, and click next, it jumps to the middle of the next group.
Is there any way to fix this? Only have it jump to whatever page you are on plus the number of links displayed (ex: page 1, 10 links displayed, Next = page 11)?
on August 15th, 2007 at 6:20 am
Saved me a few mins to fix an old project I didn’t really want to maintain :c), much appreciated.
Small mod to handle 0 being passed as the current page (Passing 0 causes probs in getSqlLimit()):
function SetCurrentPage($reqCurrentPage){
$reqCurrentPage = (integer) $reqCurrentPage;
if ($reqCurrentPage currentPage = $reqCurrentPage;
}
on August 15th, 2007 at 6:21 am
Ate my formatting, trying again:
function SetCurrentPage($reqCurrentPage){
$reqCurrentPage = (integer) $reqCurrentPage;
if ($reqCurrentPage < 1){
$reqCurrentPage = 1;
}
$this->currentPage = $reqCurrentPage;
}
on August 28th, 2007 at 4:13 pm
Having trouble getting the class to ONLY display x lines. I will display links to the number of pages, but it will show all the rows on one page.
Here is what I have:
$PaginateIt = new PaginateIt();
$PaginateIt->SetItemsPerPage(5);
mysql_select_db($database_server, $server);
$orderby = isset( $_GET[ ‘orderby’ ] ) ? $_GET[ ‘orderby’ ] : “ID”;
$getorder = isset( $_GET[ ‘order’ ] ) ? 1 - $_GET[ ‘order’ ] : 1;
$order = $getorder ? “ASC” : “DESC”;
$result = mysql_query(”SELECT * FROM “. $pagename. ” ORDER BY “. $orderby .” “.$order.”");
$fields_num = mysql_num_fields($result);
$PaginateIt->SetItemCount($result);
Where did I go wrong
on September 28th, 2007 at 11:32 pm
injecting valium
injecting valium
on October 3rd, 2007 at 4:40 pm
body causing hot in sensation valium
body causing hot in sensation valium
on October 5th, 2007 at 3:37 pm
big sausage pizza video clip
big sausage pizza video clip
on October 12th, 2007 at 9:02 pm
best detox diets
best detox diets
on October 16th, 2007 at 5:52 pm
best otc diet pill
best otc diet pill
on October 21st, 2007 at 4:35 pm
your way to beautiful gir. Horsa Amadeo.
on October 24th, 2007 at 5:12 pm
Amazing pagination script! Makes everything so much easier. I just have one question/problem. Here’s what i’m doing…
$pages->SetItemsPerPage(2);
$pages->SetLinksToDisplay(10);
$pages->SetLinksFormat( ‘>’ );
$all_query = result_src(get_most_recent());
$all_results = mysql_num_rows($all_query);
$pages->SetItemCount($all_results);
/
$sql = get_most_recent() . $pages->GetSqlLimit();
$result = result_src($sql);
echo “” . $pages->GetPageLinks() . “”;
where get_most_recent() just returns the raw sql string with no limit in it. The result_src() function just puts mysql_query into a function so that I can modify the error output for multple queries all at once and then returns the result array back to the script.
The pages display the correct number of items and paging works ok, the only problem is for example I have…
1 | 2 | 3 |4… NEXT >>
and I am currently on page 1, the link for next takes me to page like 10 not page 2, am I doing something wrong?
on October 24th, 2007 at 5:38 pm
PAGE JUMP FIX
For those that want the Next and Prev buttons to just go 1 page, instead of the bigger jump that it does by default just change this part in the paginate class.
make this line –>$pageNum = $start - $this->linksToDisplay + $linksPad;
be this line…
$pageNum = $this->currentPage - 1;
and you need to do this for back an next with obviously the next one being +1
Hope this helps
on November 9th, 2007 at 10:23 am
.
.
on November 16th, 2007 at 7:24 pm
aw
aw
on November 25th, 2007 at 1:24 pm
Plz update http://www.bradyvercher.com/Scripts/PaginateIt/PaginateIt.zip
on December 3rd, 2007 at 5:29 am
Thankiossk
Cool!
on December 6th, 2007 at 2:19 pm
lol
thankiossp!
on December 18th, 2007 at 12:03 pm
Great code. Thanks.
on December 18th, 2007 at 4:04 pm
I feel like a bunch of nothing. I guess it doesn’t bother me. More or less not much exciting happening right now. I’ve basically been doing nothing. I haven’t gotten much done these days.
on December 18th, 2007 at 6:41 pm
I’ve just been staying at home waiting for something to happen. I just don’t have anything to say right now. My life’s been basically unremarkable , but eh. I can’t be bothered with anything recently.
on December 19th, 2007 at 12:42 pm
Thanks
on December 19th, 2007 at 10:53 pm
I dunno if I am doing something wrong, but I am assuming I am. I keep getting returned just 2 pages of pagination, with 10 items as limit. This happens even when there isnt 10 items.
On one query, I have about 58 results yet still 2 pages being listed. Here’s what Ive been doing so far…
// Our pagination class…
require_once (__CLASSES__. ‘/paginator.class.php’);
// Generate our query …
$query = “SELECT COUNT(*) FROM sections”;
$result = mysql_query($query) or die (mysql_error());
// Pagination class
$pages = &new Paginator;
$pages->items_total = $result;
$pages->mid_range = 5;
$pages->paginate();
// Generate our query …
$query = “SELECT secid, secname, description FROM sections”
. “\n ORDER BY secid ASC” . $pages->limit;
$result = mysql_query($query) or die(mysql_error());
$numofrows = mysql_num_rows($result);
// Display pagination results and other navigation
echo “”
. “\n ” .$pages->display_pages()
. “\n ” .$pages->display_jump_menu()
. “\n ” .$pages->display_items_per_page()
. “\n \n”;
I cut a bit out inbetween there which was just rows being printed in a nice table. Could the fact that I am using $query on both queries and $result on both as well be having some effect?
on December 23rd, 2007 at 12:11 pm
Hi,
This is a great pagination class, but i need some help to go with friendly url’s.
My site produces links like domain.tld/search/word+to+search/page_number.
How am I able to create links like …href=”/search/search+term/12″… ?
Thanks
Nuno
on January 11th, 2008 at 2:18 pm
Nick,
Any chance you have an updated copy of the class? I tried applying the updates to GetPageLinks() from your previous comments but they appeared to have been cut off.
Thanks,
Dan
on February 8th, 2008 at 6:33 pm
slot for free
residential techniques wondrously introvert:dreadnought
on February 13th, 2008 at 2:28 am
black banana casino
weakest burntness Tenex Cretaceous gelled wilting
on February 14th, 2008 at 2:22 pm
I’m trying to get this working with a search feature, but it’s not working. My search grabs an array of variables so I am a little stuck here.
Main Array to grab Input search vars
// Start Code
$clause = array();
if ($nb_author != “”) {
$clause[] = “nb_auth LIKE ‘%$nb_author%’”;
}
if ($nb_subj != “”) {
$clause[] = “nb_subject LIKE ‘%$nb_subj%’”;
}
if ($nb_num != “”) {
$clause[] = “nb_number LIKE ‘%$nb_num%’”;
}
if ($nb_type != “”) {
$clause[] = “nb_type LIKE ‘%$nb_type%’”;
}
$clause = implode(’ AND ‘, $clause);
include(’../_resources/PaginateIt.php’);
$PaginateIt = new PaginateIt();
$PaginateIt->SetItemsPerPage(10);
$PaginateIt->SetLinksToDisplay(5);
$PaginateIt->SetLinksFormat( ‘>’ );
$PaginateIt->SetItemCount($nb_result);
$sql = “SELECT * FROM Notebooks WHERE $clause ORDER BY nb_number ASC”;
$result=mysql_query($sql, $db_link);
$nb_result = mysql_num_rows($result);
// End Code
This returns page numbers, but when I click on any page besides 1, I loose the query and my results return 0.
on February 14th, 2008 at 3:10 pm
btw, on your example page, you should encase the code you used for that in tags because its useless as an example if all we see is the outputted HTML.
on February 17th, 2008 at 8:23 am
geiko auto insurance
breaching desired biweekly
on February 20th, 2008 at 7:15 am
bingo games in laughlin nv
clobbered subnet arguably joust
on February 21st, 2008 at 12:21 pm
Great class! Got it going in about 5 minutes. Keep up the good work.
on April 22nd, 2008 at 5:47 am
stupid
on July 16th, 2008 at 2:25 am
[…] ginate database results Pagination: Easy as PREV 1 2 3 NEXT PHP Pagination with MySQL PaginateIt […]
on July 20th, 2008 at 4:50 am
master club casino
symbolization threatening preach metaphysically
on September 1st, 2008 at 1:27 am
Hello!,