ZendFramework implements the message book paging function (with demo source code download)

Source: Internet
Author: User
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;?> "> <> 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.