This article mainly introduces how ZendFramework implements the paging function of message book. It analyzes in detail the Controller action, paging style and template calling techniques involved in ZendFramework's implementation of message book, and attaches the demo source code for readers to download and reference, for more information about Zend Framework, see the following example. We will share this with you for your reference. The details are as follows:
The paging function here uses the component Zend_Paginator to implement the message book paging... Here I also refer to the tutorial written by a PHPer in PHPCHINA.
The environment I implemented and the project directory arrangement were written on the basis of the third tutorial .. if a friend doesn't understand the directory or anything .. please use ZF1.6.0 or above. please find this tutorial in your previous article... I won't talk much about it here .. thank you ..
Step 1: In our controller, that is, indexController. in php, find the indexAction. we can see that .. in this action. the relevant tutorial is to retrieve data .. now we can rewrite this Action to the following form .. the following code (with annotations ):
Function indexAction () {$ message = new message (); // instantiate the database class // get all the messages getAllMessage, getAllReMessage // the two methods are in Model (Message. php) defines // get all response data $ this-> view-> arrReviews = $ message-> getAllReMessage (); $ page = 1; // high-configuration items page $ numPerPage = 3; // number of items displayed on each page if (isset ($ _ GET ['page']) & is_numeric ($ _ GET ['page']) {$ page = $ _ GET ['page']; // retrieve the page number from the URL} $ array = $ message-> getAllMessage (); // retrieve all the message data $ paginator = Zend_Paginator: factory ($ array ); $ paginator-> setCurrentPageNumber ($ page)-> setItemCountPerPage ($ numPerPage); $ this-> view-> paginator = $ paginator; echo $ this-> view-> render ('header. phtml'); // display the template header file echo $ this-> view-> render ('message/index. phtml'); // display the template echo $ this-> view-> render ('footer. phtml'); // display the script file of the template}
Step 2: Set the pagination style. Here we use an HTML to set the pagination style .. in the Zend Framework manual. three paging display methods are provided... you can check their usage by yourself .. it's actually very simple .. I used its first one. create a new template page pagestyle under the views/scripts/directory. phtml .. this template page is the same as the message header. phtml and footer. phtml at the same level .. this paging method may be used in the future .. so I put it here ..: pagestyle. the phtml code is as follows: (Note: Please go to your entry file index here. php defines your WEB_ROOT as a global variable. It is the root directory of your website !) :
If ($ this-> pageCount):?> Class = "paginationControl"> if (isset ($ this-> previous):?> "Index /? Page = previous;?> "> <Previous Page | else:?> Class = "disabled"> <Previous Page | endif;?> Foreach ($ this-> pagesInRange as $ page):?> If ($ page! = $ This-> current):?> "Index /? Page = "> $ page;?> | Else:?> = $ Page;?> | Endif;?> Endforeach;?> If (isset ($ this-> next):?> "Index /? Page = next;?> "> Next page> else:?> Class = "disabled"> next page> endif;?> Endif;?>
Step 3: Find the index. pthml template page on the display page of the message book:
foreach($this->messages as $message): ?>
Replace this
if (count($this->paginator)): ?> $i=1; foreach ($this->paginator as $message): ?>
Then, we will add a pagination at the end:
= $this->paginationControl($this->paginator,'Elastic', 'pagestyle.phtml'); ?>
In this way, we can see the success of our message page.
Click here to download the complete instance code.