Home > Article > Backend Development > Zend Framework implements guestbook paging function (with demo source code download), zenddemo_PHP tutorial
This article describes how Zend Framework implements the guestbook paging function. Share it with everyone for your reference, the details are as follows:
The paging function here uses the Zend_Paginator component to implement guestbook paging... Here I also refer to the tutorial written by a PHPer in PHPCHINA
The environment I implemented and the directory arrangement of the project are based on the third tutorial. If there are friends who don’t understand the arrangement of directories, please use ZF1.6.0 or above. Please refer to the previous article. Find this tutorial...I won’t say much here..Thank you..
Step one: Find the indexAction action in our controller, that is, indexController.php. We can see that...in this action. Our related tutorials only get data...We now put this action ( Action) is rewritten into the following form. The following code (with comments):
function indexAction() { $message=new message();//实例化数据库类 //取到所有留言getAllMessage,getAllReMessage //二个方法在Model(Message.php)里定义的 //取到所有回复数据 $this->view->arrReviews=$message->getAllReMessage(); $page =1;//高置默认页 $numPerPage = 3;//每页显示的条数 if(isset($_GET['page']) && is_numeric($_GET['page'])){ $page = $_GET['page'];//取到URL传过来的页数码 } $array=$message->getAllMessage();//取到所有留言数据 $paginator = Zend_Paginator::factory($array); $paginator->setCurrentPageNumber($page) ->setItemCountPerPage($numPerPage); $this->view->paginator = $paginator; echo $this->view->render('header.phtml');//显示模版头文件 echo $this->view->render('message/index.phtml');//显示模版 echo $this->view->render('footer.phtml');//显示模版脚文件 }
Step 2: Get the paging style we want. Here is a HTML to set the paging style. In the Zend Framework manual, three ways of displaying paging are provided... You can take a look at them yourself The usage... is actually very simple... I used the first method. We will create a new template page pagestyle.phtml in the views/scripts/ directory. This template page is the same as the guestbook header.phtml and At the same level as footer.phtml.. Because we may use this paging method in the future.. So I will put it here..: The pagestyle.phtml code is as follows: (Note: Please add the index in your entry file here. .php defines your WEB_ROOT as a global variable, which is the root directory of your website!):
if ($this->pageCount): ?> class="paginationControl"> if (isset($this->previous)): ?> "index/index/?page= previous; ?> ">< 上一页 | else: ?> class="disabled">< 上一页 | endif; ?> foreach ($this->pagesInRange as $page): ?> if ($page != $this->current): ?> "index/index/?page= ">$page; ?> | else: ?> = $page; ?> | endif; ?> endforeach; ?> if (isset($this->next)): ?> "index/index/?page= next; ?>">下一页 > else: ?> class="disabled">下一页 > endif; ?> endif; ?>
Step 3: Find the index.pthml template page of the guestbook display page and change the original:
foreach($this->messages as $message): ?>
Replace this with
if (count($this->paginator)): ?> $i=1; foreach ($this->paginator as $message): ?>After
, we will add a paging display at the end:
= $this->paginationControl($this->paginator, 'Elastic', 'pagestyle.phtml'); ?>
In this way... we can see that our message pagination is complete
Click here to download the complete example code from this website.
Readers who are interested in more zend-related content can check out the special topics of this site: "Zend FrameWork Framework Introductory Tutorial", "php Excellent Development Framework Summary", "Yii Framework Introduction and Summary of Common Techniques", "ThinkPHP Introductory Tutorial" , "php object-oriented programming introductory tutorial", "php mysql database operation introductory tutorial" and "php common database operation skills summary"
I hope this article will be helpful to everyone’s PHP programming based on the Zend Framework framework.