Adding Control to the Paginator

The paginator control allows you to display not only a next or previous button the user can use to move through the result set, but also provides functionality that allows you to display the total number of pages. This can be accomplished by using the paginationControl() method of Zend_Paginator. The function accepts three parameters:

• The initial parameter accepts a Zend_Paginator object.

• The second parameter accepts a string value representing the type of style to implement. Acceptable values are shown in Table 5-7.

• The third parameter accepts a string value representing the location of the view to use for the pagination control.

Table 5-7. Pagination Controller Styles





Displays all the pages available to the user regardless of whether you use



Displays an initial set of pages. As the user moves from page to page, the number of

pages displayed on the paginator control will expand.


Displays a given range; moves on to the next range when the user gets to the end of

the first range.


Displays the page range set using setPageRange(). Places the current page number as

close to the center as possible.

Each style displays the content and interacts in different ways with the user. Try out the different combinations to see which one you like.

Table 5-8. Zend_Paginator Properties



Return Type


Returns the initial page number in the set of pages.



Returns the index of the first record in the result set on the current page.



Returns the first page in the scrolling range for the current page.



Returns the current page number in the scrolling page range.



Returns the current number of records displayed in the current page.



Returns the total number of records that can be displayed per page.



Returns the last page number in the total set of pages.



Returns the index of the last record on the current page.



Returns the last page number in the current scrolling page range.


Returns the next page number to be fetched.

String pageCount

Returns the total number of pages.

String pagesInRange

Returns all the page numbers to display in the current scrolling Array page range. Key-value array: Value represents the page number.


Returns the previous page number.

String totalltemCount

Returns the total number of records to paginate.


Using both tables, let's add a paginator control to the list.phtml view. You now need to create the view that the pagination control will implement. The view contains the look and feel for the pagination control and is the file that you need to modify to make the control look the way you want. Create a new file, paginationcontrol.phtml, in the directory includes/ and copy the code shown in Listing 5-38. The file is not required to live here; you are just saving it to this location to easily reuse the view for other paginator instances.

Listing 5-38. Pagination Control View <?php if($this->pageCount){ ?>

<!-- ADD PREVIOUS BUTTON --> <?php if(isset($this->previous)){ ?>

<td><a href="list?i=<?php echo $this->previous?>"> &lt;-

<!-- SHOW THE AVAILABLE PAGES --> <?php foreach($this->pagesInRange as $page){

<a href="list?i=<?php echo $page?>"><?php echo $page; ?></a> I

<td><?php echo $this->current; ?> I</td>

<!-- ADD NEXT BUTTON --> <?php if(isset($this->next)){ ?>

<td><a href="list?i=<?php echo $this->next?>">Next -&gt;</a></td> <?}?>

The pagination control shown in Listing 5-38 checks whether there are pages to paginate. If so, create the controls that contain a previous button and a next button. To display the number of pages the user can view at a single time, use the foreach() iteration function and iterate through each page. (Of course, you are not tied down to this layout and can design the pagination any way you want as long as you use the properties in Table 5-8 to control and display pages.)

Integrating the pagination control requires a single line to add anywhere you want to display the controls. Open the views/scripts/artist/list.phtml file and update the file as shown in Listing 5-39.

Listing5-39. list.phtml Update: Using paginationControl

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

<html xmlns="" xml:lang= "en" lang="en">

<?php echo $this->headTitle(' - Artist List'); ?> </head> <body>

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

echo " <tr><td><b>Total Artists:</b> ".


foreach($this->paginator as $item){

echo "<tr><td>".$item."</td></tr>";

echo $this->paginationControl($this->paginator, 'Sliding',


echo "</td></tr>"; }else{?> <tr>

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

Listing 5-39 builds on the example shown in Listing 5-38. Unlike the previous example, you append the call to render the paginator's control. You use the paginationControl() method to do so and pass in three parameters. The initial parameter is the object you created in the listAction: $paginator. The second parameter is the scrolling style from Table 5-7. The third parameter is the location of the pagination control view; in the example, it's in the includes folder. Load the URL http://localhost/artist/list to

Was this article helpful?

0 0

Post a comment