如何使用zend分页

时间:2014-12-06 14:48:33

标签: php database zend-framework pagination zend-paginator

我想在zend framework1中加入分页,
在模型中,我已经宣布 Books.php (application / models / books.php):

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

在控制器中,我有 BookControllers.php (application / controllers / BooksControllers):

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

并且在视图中,我曾用Table来显示数据库查询( 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>

如何以每页10个记录限制分页信息?

1 个答案:

答案 0 :(得分:1)

尝试更改控制器的操作代码:

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;
}

然后您可以像以前一样使用您的视图,但是 - 您可能需要按钮来在页面之间导航。 Here's基本的paginator partial,使用你在Zend docs中提到的视图:

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

请参阅Zend_Paginator docs - 有关此课程的许多问题都有答案。