GMgKe586q6suSQnyqZLlGCooeWM

Pages

Search

Monday, April 18, 2011

Zend - Paginator

<?php

$request = $this->getRequest();
$page = $request->getParam('page');
if(!isset($page) || $page<1) $page=1;

$registry = Zend_Registry::getInstance();

$db = $registry['db'];
$result = $db->fetchAll("SELECT * FROM data");



// the core paginator
$paginator = Zend_Paginator::factory($result);
$paginator->setItemCountPerPage(10);
$paginator->setCurrentPageNumber($page);
$this->view->paginator = $paginator;

if($page==1){
    $this->view->previous = $page;
}else{
    $this->view->previous = $page-1;
}

$this->view->next = $page+1;


?>


Then on your view, use:

<?php include "header.phtml";?>
<?php
    $output = '<ul>';
    foreach($this->paginator as $row){
        $output .= '<li>'.$row['id'].' '.$row['name'].' '.$row['city'].' '.$row['age'].'</li>';
    }
    echo $output.'</ul>';
?>
<a href="<?php echo $this->baseUrl();?>/index/front/page/5">5</a> |
<a href="<?php echo $this->baseUrl();?>/index/front/page/<?php echo $this->next;?>">Next</a> |
<a href="<?php echo $this->baseUrl();?>/index/front/page/<?php echo $this->previous;?>">Previous</a>
<?php include "footer.phtml";?>
Share/Bookmark

No comments:

Post a Comment