Perfect PHP Pagination

Maybe it’s just me, but doesn’t this pose a massive performance issue? Selecting all of the records, only to dump the rest and keep only 10 seems like a pretty massive waste to me. Obviously you’d be doing a query for this sort of thing, because 9 times out of 10 that’s when you’d be paginating data.

check that link… loads easier, although not as customizable

I agree with dmsuperman. The pagination needs to go a little more detailed in the query portion of it. I would not want to query all the results to show only a fraction of them.

What I usually do is get a count of all the records that match my query, based on what ever conditions I’m looking for, then use the page I wish to display for, and the number of results for that page, and use those three points of information to determine how I’m going to limit my result. I calculate the offset for my LIMIT for my query based on the page number and the total I want to display per page, and do my query on that, to get exactly the number of records I need.

With MySQL you can even do this with a single query. Check out SQL_CALC_FOUND_ROWS and FOUND_ROWS() (

To be absolutely precise, you still run two queries, but the second one merely reads a stored variable, which is extremely fast.

Fatal error: Interface ‘PageLayout’ not found in [path]\DoubleBarLayon line 4
I have included the

require_once ‘PageLayout.php’;
require_once ‘DoubleBarLayout.php’;

in the index.php!!

Any help would be great!!




Did you get this fixed? I have the same problem!!

kind Regards



most likely you have you php.ini file set to force the long version of the php open tag.
Look for the following in your php.ini to be sure.
short_open_tag = Off

That being the case you need to edit the Paginated.php and PageLayout.php files and make sure the open tag is ‘<?php’. After that the example should run smoothly.


thanks for this useful article.

The most flexible and easiest to implement pagination class I have seen so far. Brilliant.

pagination using phpcode+mysql,html code

Your thoughts on using a default strategy or explicitly have the user define it in the constructor of Paginated class rather use setLayout()?

Interesting now in ZF 1.6 is the Zend_Paginator. It works on the notion of a strategy for the data to be passed and accepts arrays and a db select object

	$pageNumber = $this-&gt;_getParam('page') ? $this-&gt;_getParam('page') : 1;

->limitPage($pageNumber, 10);

//set page number
$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($select));

$this->view->paginator = $paginator;

//the above is available in your view
//<?=$this->paginator?> will echo page links

Extremely useful on small result sets, but I think this would slow down pages with very large result sets. An approach similar to the Zend_Paginator may be applied by modifying the Paginated class to accept SQL SELECT strings.

wel coding.i like this type of templates.i will say it is nice coding.

it is good coding but i want to learn more easy steps!

as OJ mentioned, this is not good with huge result set. In thousand result set, this will surely pretty slow, what more with millions of records? Don’t like to recommend this one.

Can this code be amended to use a set of ID’s within the array as the pagination instead of page numbers?

i.e instead of ?page=2 you would have ?imageid=1610, the resulting pagination thus forming permalinks.

Notice: Undefined offset: 8 in C:\workspace\ urnos\hosting\scr\modelo\paginacion\Paginated.php on line 100 i have this problem when i have 8 items in my array and i show 5 elemens per page, the page 1 show ok but the second show this notice

Nice tutorial as usual, but i have to be honest, it’s not really that easy to follow. Maybe i’m experiencing a blond moment, and i’m sure i could just download the code and play, but the run down on instructios need to be put into better novice terms or perhaps less use of web language terms would make this a little easier to read.

I’m still learning PHP myself (constantly - it’s never ending, lol), and although it makes sense for the most part, i can’t help but feel baffled by what seems should be fairly straight-forward task.

I’d suggest just this, pass along your article to a family member, someone with little or no experience and ask them if what they read makes sense, if not then perhaps rethink the wording or terms used.

I’ve read many articles here, and it’s an absolute gem of a resource, but i felt this one a little lacking in the breakdown. Of course that’s not to say it’s a bad article, it’s great work, just left a little high and dry with the “idiot proof” run down on code.

We idiots need it in simple terms to, even if the above seems simple enough… does that make sense

All the best to sitepoint peeps for 2009 anywho!!..

add this at line 101 or so of Paginated.php (replace what’s there) - should fix the error.

return $this->rs[$index];
return false;