Perfect PHP Pagination

Notice: This is a discussion thread for comments about the SitePoint article, Perfect PHP Pagination.


Where’s the code download?

End of “Required Files” section says “(link to code download)” but doesn’t actually display a link to the code download

It would been nice to see this strategy applied to some a little more complex than arrays to something like database queries. The Paginated class requires that you pre-fetch the data into an array which puts needless performance overhead on your script/server. Generally speaking, the 3 primary needs for pagination are arrays, file systems and database results.

You’ve gone to lengths to separate the page layout from the data, but haven’t bothered abstracting the data source for some reason.

Should be here: http://www.sitepoint.com/examples/pagination/paginated-demo.zip

Gives all the required files for the article

spikeZ

the link is good.I have tried it and I am getting the result with success
But if I am having query string in my url so is there any way to build the query string of url in the class itself?

I havent had chance to look at the code itself Babara so I cant tell you for sure.

I have googled and found the mypagina script working for building query string with pagination.
Have a look at it.
Mypagina script

Thanks for pointing this out.

A strategy for the result set will be great where each implements how to retrieve a single record or a set.

barbara1712, fetchPagedNavigation can be modified to accept an array which will be the global $_GET and iterated to form the query string.

Both modifications will be great and as time permits I will implement suggestions.

Download the attached files for an abstraction of the data sources. In this case arrays and mysql result set. Strategy pattern used.

run index.php to see demo. Alter script to connect to database and query one of your tables.

data source now abstracted. You can add implementations for files and other result set types

I always envisaged pagination as a decorator actually. It acts very much like a filter (which in java is modelled as a decorator). So you’re filtering results down to a smaller set based on a couple of parameters. It would also follow true that filtering by first letter would again be a filter which would act on your original object.

I am querying one of my tables for two values and its only displaying the first value of the first row and there are 23 rows returned by the query. Can you give an example of how to alter your script to use a query?

Is it possible to get an example of how to implement row/column limits within the pages. For example I need to have 5 columns and 4 rows per page max.

Any help is greatly appreciated.

The Pager PEAR class is a better way to go about with this.

Well I don’t want to get some poeple upset here but I guess this (and the named PEAR class too) is not really what should be considered to be perfect for a pagination class. Its all still too rendering related what the classes do.

If you don’t have a problem with mixin’ business logic and render stuff it should be fine though I suppose.

Otherwise one would opt for a more separated approach that is just in charge of generating some pagination urls and/or page numbers for the pagination instead of being in charge of rendering the whole list of records too.
This way everything is just more loosely coupled.

This whole thing might look nice, but it’s unneccessarily complicating things for something as simple as pagination.

Apart from that it’s not practicable! You have to pass the whole result set to the class to get the pagination done… That might work with a list of 20 names, but definitely NOT with hundreds or thousands of database entries of any kind.

It wastes not only memory but also cpu power loading ALL the results, where you only need a piece.

Doesn’t work:

Interface ‘PageLayout’ not found in [path]/DoubleBarLayout.php on line 2

How can i use this script to display a table?

Forbes add in index.php these 2 lines
require_once ‘PageLayout.php’;
require_once ‘DoubleBarLayout.php’;

for Jose

$sql = “SELECT idRow,firstName,lastName FROM yourTable”;
$resSql = mysql_query($sql);

$page = $_GET[‘page’];
$pagedResults = new Paginated(new PaginatedMysqlResultSet($resSql), 2, $page);
$pagedResults->setLayout(new DoubleBarLayout());

while ($row = $pagedResults->fetchPagedRow())
{
echo $row[‘firstName’];
}

<td><?php echo $pagedResults->fetchPagedNavigation();?></td>

@raduku
i’m getting this error when i use the code below
Fatal error: Class ‘PaginatedMysqlResultSet’ not found in /home/.mysite/example.com/index.php on line 98

can somebody help?

After session_start() in index.php put this lines in this order
require_once ‘Paginated.php’;
require_once ‘PaginatedResultSet.php’;
require_once ‘PaginatedMysqlResultSet.php’;
require_once ‘PageLayout.php’;
require_once ‘DoubleBarLayout.php’;

if this files are in the same directory as index.php, if there are not make sure the path (relative or absolute) is correct.

What do you have on line 98?

am i missing something? pardon my stupidity.
i didn’t get these files in the src download:

require_once ‘PaginatedResultSet.php’;
require_once ‘PaginatedMysqlResultSet.php’;

also, this is what is on line 98
$pagedResults = new Paginated(new PaginatedMysqlResultSet($resSql), 2, $page);

If you downloaded the attached files by zbap, you’ll find them there. :slight_smile: