How to use zend pagination

351 Views Asked by At

I want to pagination in zend framework1,
in models , I've declared Books.php (application/models/books.php):

<?php
class Model_Books extends Zend_Db_Table_Abstract
{
    protected $_name = 'subscriber';
}

in controllers , I have this BookControllers.php (application/controllers/BooksControllers):

<?php
class BooksController extends Zend_Controller_Action
{
    public function listAction() 
    {
        $booksTBL = new Model_Books();
        $this ->view->books= $booksTBL->fetchAll();
    }
}

and in view, I used to Table to show database query (list.phtml)

<?php echo $this->doctype() ?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>لیست</title>
  <?php echo $this->headLink()->appendStylesheet('/css/global.css') ?>
</head>
<body>
 <div class="datagrid">
    <table style="border-collapse: collapse; text-align: center; width: 100%;">
      <tr>
        <th>id</th>
        <th>username</th>
        <th>domain</th>
        <th>password</th>
        <th>email address</th>
        <th>ha1</th>
        <th>ha1b</th>
      </tr>

        <?php 
            foreach ($this ->books as $key =>$value)
            {

                echo '<tr><td>'.$value->id.'</td><td>'.$value->username.'</td><td>'.
                $value->domain.'</td><td>'.$value->password.'</td><td>'.$value->email_address
                .'-</td><td>'.$value->ha1.'</td><td>'.$value->ha1b.'</td></tr>';
            }
        ?>
  </table>
 </div>
</body>
</html>

How to pagination information with 10 record limitation per page?

1

There are 1 best solutions below

0
On BEST ANSWER

Try changing a bit your controller's action code:

public function listAction() 
{
    $request = $this->getRequest();
    $page = $request->getParam('page', 1);

    $booksModel = new Model_Books();
    $select = $booksModel->select();
    $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbTableSelect($select));
    $paginator->setCurrentPageNumber($page);
    $paginator->setItemCountPerPage(10);

    $this->view->books = $paginator;
}

Then you can use your view like before, however - you'll probably need buttons for navigation between pages. Here's the basic paginator partial, use it your view like mentioned in Zend docs:

<?php echo $this->paginationControl($this->paginator,
                                    'Sliding',
                                    'my_pagination_control.phtml'); ?>

See Zend_Paginator docs - there are answer for many question related to this class.