Pager Solutions

2018-07-13 09:30:55
tengfei
734
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();?>

Write a Comment
Comment will be posted after it is reviewed.