Using the Zend Paginator

The Zend_Paginator can be used by loading the Zend_Paginator class into a PHP file.

After the class is loaded, you can instantiate a Zend_Paginator object using its constructor and supply it with a Zend_Paginator_Adaptor object. The supported adaptors are the following:

• Zend_Paginator_Adaptor_Array

• Zend_Db_Table_Select

Depending on the type of data you want to paginate, use the required adaptor. If you want to paginate data for an array, use the Zend_Paginator_Adaptor_Array class. To use database result sets, you can create either a Zend_Db_Select object or a Zend_Db_Table_Select object. You can also allow Zend_Paginator to automatically create the adaptor for you by using the Zend_Paginator::factory() method and pass in an array or the object you want to use.

The Zend_Paginator provides additional functionality to manipulate the data presented to the user. These methods are shown in Table 5-6.

Table 5-6. Zend_Paginator Setters

Method

Description

setCurrentPageNumber()

Sets the current page number; the default is 1.

setItemCountPerPage()

Sets the number of records to display per page; the default is 10.

setPageRange()

Sets the total number of pages to display in the pagination control; the

default is 10.

setView()

Sets the view that you want to associate with this pagination.

Aside from providing helpful methods, Zend_Paginator requires interaction by the user. The user must click a given page number to inform the Zend_Paginator what set of records it needs to fetch. Depending on the current page the user is on, a different set of records will be displayed. To determine which page the user is requesting, you need to use a query to pass in the data. Retrieving the page number is then something the PHP file must capture using the Request object.

Using the table, let's work on a small example that will use the methods and the factory() method. What you'll need is an action and a view. Open the ArtistController.php file and create listAction(). The action will contain the pagination example shown in Listing 5-36.

Listing5-36. ArtistController.php: listAction()

* Display all the Artists in the system.

public function listAction(){

//Create a sample array of artist

$artist = array("Underworld", "Groove Armada", "Daft Punk", "Paul Oakenfold", "MC Chris", "Ramones", "The Beatles", "The Mamas and the Papas", "Jimi Hendrix");

//Initialize the Zend_Paginator

$paginator = Zend_Paginator::factory($artist);

$currentPage = 1;

//Check if the user is not on page 1

$i = $this->_request->getQuery('i'); if(!empty($i)){ //Where i is the current page

$currentPage = $this->_request->getQuery('i');

//Set the properties for the pagination $paginator->setItemCountPerPage(2); $paginator->setPageRange(3); $paginator->setCurrentPageNumber($currentPage);

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

Listing 5-36 demonstrates the new listAction(). The method begins by initializing the data you'll paginate through. It's a set of nine electronic music artists, all contained in an $artist array. Once initialized, you create a Zend_Paginator object and use its factory method to pass in the data to paginate. Because you're using an array, this will become a Zend_Paginator_Adaptor_Array object behind the scenes.

You now need to determine what page the user is on. To do this, pass a query value using the URL. The query value is represented by the variable i, and the URL will look like this: http://localhost/artist/list?i=<pagenumber>. If the variable i is not set, you know that the user is in the initial page, and you can use the default value you set as 1. Finally, set the total number of artists to display in a single page, 2; set the number of pages to display in the paginator control, 3; and set the page the user is currently loading for this request.

Create a new file in the views/scripts/artist directory and call it list.phtml. The view will render the records. Copy the code shown in Listing 5-37 into the list.phtml file.

Listing5-37. list.phtml

<?php echo $this->doctype('XHTML1_STRICT'); ?>

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<?php echo $this->headTitle('LoudBite.com - Artist Listing'); ?> </head> <body>

<?php echo $this->render("includes/header.phtml")?>

foreach($this->paginator as $item){ echo "<tr><td>".$item."</td></tr>";

<tr><td>There were no artists present in the system. Add one now!</td></tr>

Listing 5-37 demonstrates how to display the content to the user using a view. Because the collection to paginate is stored into Zend_Paginator, which uses the Iterator class, you can use a loop function such as foreach to iterate through the record set. Go ahead and try it out. Load the URL http://localhost/artist/list to return the first two records and then try http://localhost/artist/list?i=3 to return records 5 and 6 from the artist variable.

Now you need a way to allow the user to move through a set of records by clicking either next or previous links or buttons; you need a paginator control.

Was this article helpful?

0 0

Post a comment