- 1 Introduction
- 1.1 About zentaoPHP
- 1.2 Features
- 1.3 License
- 2 Installation
- 2.1 System Requirement
- 2.2 Install zentaoPHP
- 3 Quick Start
- 3.1 Echo Hello World!
- 3.2 Use MVC to echo Hello World!
- 3.3 Example: Deploy the blog built in zentaoPHP
- 4 Basics
- 4.1 Basic Concepts
- 4.2 Request Types
- 4.3 Create Links
- 4.4 Class: HTML, JS, and CSS
- 5 Advanced
- 5.1 Directory Structure
- 5.2 DAO
- 5.3 Pager Solutions
- 5.4 Data Validation
Pager Solutions
- 2018-07-13 09:30:55
- tengfei
- 6189
- Last edited by tengfei on 2019-09-16 14:11:43
Paging is a common problem for database-based applications. In zentaoPHP, a built-in pager is included.
Take an example of query in a user list. Set up a user module in the application, define a browse method in control file to perform as the pager :
1. Three parameters in browse
The browse method requires to define three parameters: recTotal, recPerPage, and pageID. The variable name is fixed.
public function browse($recTotal, $recPerPage, $pageID) { /* load pager class and generate pager objects*/ $this->app->loadClass('pager', $static = true); $pager = new pager($recTotal, $recPerPage, $pageID); /* pass pager class to the model and page*/ $users = $this->user->getList($pager); }
2. Invoke pager objects in model
Define a getList method in the model and receive pager objects, and calls pager ($pager) to generate pager statements when using DAO queries.
public function getList($pager) { return $this->dao->select(*)->from('user')->page($pager)->fetchAll(); }
3. Assign a pager object to a template in control
Go back to the browse method in control, and assign the pager object to the template.
public function browse($recTotal, $recPerPage, $pageID) { /* load pager class and generate pager object */ $this->app->loadClass('pager', $static = true); $pager = new pager($recTotal, $recPerPage, $pageID); /* pass pager class to model and page */ $users = $this->user->getList($pager); /* assgin to the template */ $this->view->users = $users; $this->view->pager = $pager; }
Pager link is displayed in the template: the show () method has two parameters,
$align: left, center, right. The default is right alignment.
$type: full|short|shortest
<?php $pager->show();?>