Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pagination in Zend

Friends,

I want to create pagination in Zend Framework. I'm new to ZF.

index.phtml is given below

<table>
<tr>
    <th>Name</th>
    <th>Quantity</th>
    <th>&nbsp;</th>
</tr>
<?php foreach($this->orders as $order) : ?>
<tr>
    <td><?php echo $this->escape($order->name);?></td>
    <td><?php echo $this->escape($order->quantity);?></td>
    <td>
        <a href="<?php echo $this->url(array('controller'=>'index','action'=>'edit', 'id'=>$order->id));?>">Edit</a>
        <a href="<?php echo $this->url(array('controller'=>'index', 'action'=>'delete', 'id'=>$order->id));?>">Delete</a>
    </td>
</tr>
<?php endforeach; ?>
</table>
<p><a href="<?php echo $this->url(array('controller'=>'index','action'=>'add'));?>">Add new album</a></p>

My index controller is below

class IndexController extends Zend_Controller_Action
{
    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        $this->view->title = "My Orders";
        $this->view->headTitle($this->view->title, 'PREPEND');
        $orders = new Model_DbTable_Orders();
        $this->view->orders = $orders->fetchAll();
    }
}
like image 204
Rakes Avatar asked Feb 19 '10 06:02

Rakes


2 Answers

I try to help you based on what I have in my ZF project.

So, in your class Model_DbTable_Order you can defined a method called e.g. getOnePageOfOrderEntries() as follows:

 /**
 * Return one page of order entries
 *
 * @param int $page page number
 * @return Zend_Paginator Zend_Paginator
 */
public function getOnePageOfOrderEntries($page=1) {

    $query = $this->select();
    $paginator = new Zend_Paginator(
            new Zend_Paginator_Adapter_DbTableSelect($query)
    );
    $paginator->setItemCountPerPage(100);
    $paginator->setCurrentPageNumber($page);
    return $paginator;
}

Than in indexAction you can have something like this:

 public function indexAction()
{
    $this->view->title = "My Orders";
    $this->view->headTitle($this->view->title, 'PREPEND');
    $orders = new Model_DbTable_Orders();
    //$this->view->orders = $orders->fetchAll();

    $page = $this->_request->getParam('page');
    if (empty($page)) { $page = 1; }

    $paginator = $orders->getOnePageOfOrderEntries($page);
    $this->view->paginator = $paginator;

}

In your index.phtml view you could have something similar to this:

  <?php if (count($this->paginator)): ?>
    <table>
    <tr>
        <th>Name</th>
        <th>Quantity</th>
        <th>&nbsp;</th>
    </tr>
          <?php foreach($this->paginator as $order): ?>

    <tr>

        <td><?php echo $order->name;?></td>
        <td><?php echo $order->quantity;?></td>
         <td><!-- And the rest what you want --></td>           
    </tr>

    <?php endforeach; ?>
</table>
<?php endif; ?>
   <?php echo $this->paginationControl($this->paginator,
    'Sliding','partial/my_pagination_control.phtml'); ?>

Where my_pagination_control.phtml is as follows (this is just copy-paste what I have and it is from the ZF Reference quide):

   <?php if ($this->pageCount): ?>
    <div class="paginationControl">
     <!-- Previous page link -->
    <?php if (isset($this->previous)): ?>
     <a href="<?php echo $this->url(array('page' => $this->previous)); ?>">
     Previous
      </a> <span class="bar"> | </span>
      <?php else: ?>
      <span class="disabled"> Previous</span> <span class="bar"> | </span>
<?php endif; ?>
<!-- Numbered page links -->
<?php foreach ($this->pagesInRange as $page): ?>
  <?php if ($page != $this->current): ?>
    <a href="<?php echo $this->url(array('page' => $page)); ?>">
        <?php echo $page; ?>
    </a> <span class="bar"> | </span>
  <?php else: ?>
    <?php echo $page; ?> <span class="bar"> | </span>
  <?php endif; ?>
<?php endforeach; ?>
<!-- Next page link -->
<?php if (isset($this->next)): ?>
  <a href="<?php echo $this->url(array('page' => $this->next)); ?>">
    Next
  </a>
<?php else: ?>
  <span class="disabled">Next </span>
<?php endif; ?>
</div>
<?php endif; ?>

Hope it will be useful.

like image 107
Marcin Avatar answered Nov 09 '22 03:11

Marcin


Try this http://zendgeek.blogspot.com/2009/07/zend-pagination-example.html

like image 42
Ali Avatar answered Nov 09 '22 04:11

Ali