Source : http://e-university.wisdomjobs.com/zend/
Applications can contain hundreds and even millions of records. Members of a site, artist information, a list of images.all these items can be displayed. If the application contained hundreds of records and displayed the content to the user all at once, the user would be overwhelmed with too much data at a single time. The user also might become frustrated by the time it takes for the content to load because the query would fetch all the records at once.
Applications can contain hundreds and even millions of records. Members of a site, artist information, a list of images.all these items can be displayed. If the application contained hundreds of records and displayed the content to the user all at once, the user would be overwhelmed with too much data at a single time. The user also might become frustrated by the time it takes for the content to load because the query would fetch all the records at once.
You can solve the dilemma by using pagination, which allows applications
to fetch smaller result sets there by displaying content in manageable
and faster loading portions. The result sets are usually broken up into
numbered pages. For example, if you have 1,000 records and applied
pagination, you can display the content in a set 10 pages, each page
containing 100 records. As the user continues to click on the next page,
the application retrieves the next set of data.
To add pagination to any application, you can use the Zend_Paginator
library, which has the capability to paginate any collection of data in
an array as well as result sets from a database using the Zend_Db_Select
object. It also allows you to apply any type of view to render the
content.
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_Select
- Zend_Db_Table_Select
- . Iterator
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.
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, lets work on a small example that will use the methods and the factory() method. What youll 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.
Listing 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 demonstrates the new listAction(). The method begins by initializing the data youll paginate through. Its 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 youre 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:. 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 into the list.phtml file.
Listing list.phtml
<?php echo $this->doctype('XHTML1_STRICT'); ?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<?php echo $this->headTitle('LoudBite.com - Artist Listing'); ?>
</head>
<body>
<?php echo $this->render("includes/header.phtml")?>
<table border='0' width='600'>
<?php
if ($this->paginator)
{
foreach($this->paginator as $item)
{
echo "<tr><td>".$item."</td></tr>";
}
}
else{?> <tr>
<td>There were no artists present in the system. Add one now!</td></tr> <?php } ?> </table> </body>
</html>
Listing builds on the example shown in Listing. 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. The third parameter is the location of the pagination control view; in the example, it’s in the includes folder. the resulting pagination controller and the list of artists, as shown below.

No comments:
Post a Comment