Paginating Records - 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.

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.

Table Zend Paginator Setters

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.

Listing ArtistController.php: listAction()

Listing 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:. 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

Listing 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.

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.

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 pagination Control() 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.
  • The third parameter accepts a string value representing the location of the view to use for the pagination control.

Table Pagination Controller Styles

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 Zend Paginator Properties

Table Zend Paginator Properties

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. 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 Pagination Control View

The pagination control shown in Listing 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 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.

Listing list.phtml Update: Using paginationControl

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.

Artist list with Paginator controller

Let’s look at the paginationControl() function in a bit more detail. It accepts three parameters:

  • The initial parameter is a Zend_Paginator object.
  • The second parameter, a string, is the type of style you want to use in the paginator controller (all possible values are shown in Table).
  • The third parameter accepts a string that is the location of the view that will be used. By default, paginationControl() searches in the views/scripts/ directory for the file.

You created a completely supported pagination page using all the components involved in the pagination of data. Now let’s see how to paginate database results.

Paginating Database Records

The Zend_Paginator does not only support arrays. As shown in the beginning of this section, you can also paginate through database result sets. You can accomplish this by supplying a Zend_ Db_ Select object.

By supplying a Zend_Db_Select object to the Zend_Paginator, you enable it to query only the number of records you need to display in a single page. This reduces the amount of time it takes to load a page.

Let’s update the previous example to query the database to fetch the artist from the database instead of using an array. Open the ArtistController.php file and update the code, as shown in Listing.

Listing Using Zend_Db_Select with Zend_Paginator

 /**   *  Display all the Artists in the system.   */    public function listAction(){   $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');   }  //Create Db object   require_once "Db/Db_Db.php";   $db = Db_Db::conn();  //Create a Zend_Db_Select object   $sql = new Zend_Db_Select($db);  //Define columns to retrieve as well as the table.   $columns = array("id",  "artist_name");   $table = array("artists");  //SELECT `artists`.`id`,  `artists`.`artist_name` FROM `artists`   $statement = $sql->from($table,  $columns);  //Initialize the Zend_Paginator   $paginator =  Zend_Paginator::factory($statement);   //Set the properties for the pagination   $paginator->setItemCountPerPage(10);   $paginator->setPageRange(3);   $paginator->setCurrentPageNumber($currentPage);   $this->view->paginator = $paginator; } 

The updated listAction() shown in Listing demonstrates how you can implement the Zend_ Db_Select object previously covered in this chapter in conjunction with Zend_Paginator to fetch and paginate the result set.

Start by setting the default $currentPage value. If the user does not supply a value for the variable i in the URL, use this default value. After setting the $currentPage value, load the database model and create a Zend_Db_Adaptor_Pdo_Mysql object. With the adaptor created, create the SELECT statement using the Zend_Db_Select object. The SELECT statement will fetch the ID, artist_ name, and genre for each artist in the artists table. You instantiate the Zend_Paginator object and pass in the object into its constructor, set the properties you want, and finally create the $paginator view variable.

To get the artistfs name in list.phtml, you need to access the array containing it. Therefore, change the foreach() loop to the following:

Load the URL to view the paginated result set of the query. If you do not have any artists in the table, add a few for testing.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd Protection Status

Zend Topics