如何在zend框架中创建一个简单的分页?

时间:2014-05-01 08:52:35

标签: php zend-framework pagination

我有一个项目,我需要实现分页。我试着寻找不同的时间,但找不到任何满意的结果。这就是为什么我在这里发布我的问题 -

这是控制器文件IndexController.php

这是我的indexAction,我正在尝试实现分页 -

public function indexAction() {

     $this->_helper->layout->setLayout('layout');

     //pagination
     $page = $this->_request->getParam('page');
     if (empty($page)) { 
        $page = 1; 
     }
     $paginator = $this->_objCoupon->getOnePageOfOrderEntries($page);
     $this->view->paginator = $paginator;
     //pagination ends


     $coupons = $this->_objCoupon->getAllcoupon();
     $storage  = new Zend_Auth_Storage_Session();

     $dataUser = $storage->read();
     $id       = $dataUser->id;
     //$id       = $storage->id;
     $arrCoupon = array();

     if(count($coupons) > 0) {

        $i= 0;
        foreach($coupons as $couponArray) {

            $arrCoupon[$i]["pid"]   = $couponArray['id'];
            $arrCoupon[$i]["title"] = $couponArray['partner_name'];
            $arrCoupon[$i]["img"]   = $couponArray['companylogo'];
            $arrCoupon[$i]["value"] = $couponArray['coupon_value'];

            $i++;
        }
    }
    $this->view->arrCoupon = $arrCoupon;
    $this->view->selectedcheckbox = $this->selectCheckboxes;
    $this->view->id = $id;
    $this->view->asd = 'all';
}

这是模型文件CouponModel.php

这是我的模特功能

public function getOnePageOfOrderEntries($page=1) {

    //$paginator = $this->_coupon->getAllcoupon();


    $query = "SELECT * FROM rechargecoupon WHERE online_offer = 'N' ORDER BY id ASC";
    $paginator = new Zend_Paginator(
            new Zend_Paginator_Adapter_DbTableSelect($query)
    );
    $paginator->setItemCountPerPage(20);
    $paginator->setCurrentPageNumber($page);
    return $paginator;
}

并且在视图中我现在只是尝试打印paginator变量值。

这是查看文件index.phtml

<?php if (count($this->paginator)): ?>
<?php print_r($this->paginator); die; ?>

当我执行此文件时,我收到“Class Zend_Paginator_Adapter_DbTableSelect not found error”。

2 个答案:

答案 0 :(得分:0)

您的行动中应该包含以下变量:

  • $currentPage =从网址获取的值必须为&gt; = 1
  • $itemsPerPage =硬编码值,最好取自配置
  • $itemsCount = SELECT COUNT(*) FROM ... WHERE ...
  • $pagesCount = ceil($itemsCount / $itemsPerPage)
  • $items = SELECT * FROM ... WHERE ... LIMIT ($currentPage - 1) * $itemsPerPage, $itemsPerPage,这些是要显示的项目,例如文章

在动作中设置这些变量时,应将它们传递给视图并从中生成分页,这意味着:

  • 如果页数大于1:
    • display pagination
    • 生成从1到$pagesCount = 1,2,3 ... 10
    • 的分页
    • 每个生成的分页编号都应包含与您的操作相关联的网址
  • 如果页数为1:
    • 不显示分页

答案 1 :(得分:0)

首先。 正确使用

$query = $this->select()
              ->from('rechargecoupon')
              ->where('online_offer = ?', 'N')
              ->order('id ASC')

$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbTableSelect($query));
        $paginator->setItemCountPerPage('50');
        $paginator->setCurrentPageNumber($page);

这是在模型中。

在视图中

<?php if ($this->paginator->getTotalItemCount() !== 0): ?>
// echo elements you need
<?php endif; ?>

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

pagination.phtml显示分页。 在pagination.phtml

<?php if ($this->pageCount > 1): ?>
    <div>
        <!-- Previous page  -->
        <?php if (isset($this->previous)): ?>
            <a href="<?php echo $this->url(array('page' => $this->previous)); ?>">
                <span>&lt; <?php echo 'previous'; ?></span>
            </a>
        <?php else: ?>
            <span class="disabled">&lt; <?php echo 'previous'; ?></span>
        <?php endif; ?>

        <!-- Page links -->
        <?php foreach ($this->pagesInRange as $page): ?>
            <?php if ($page != $this->current): ?>
                <a href="<?php echo $this->url(array('page' => $page)); ?>">
                    <span><?php echo $page; ?></span>
                </a>
            <?php else: ?>

            <?php endif; ?>
        <?php endforeach; ?>
        <?php if(!in_array($this->pageCount, $this->pagesInRange)): ?>
            <a href="<?php echo $this->url(array('page' => $this->pageCount)); ?>">
                <span><?php echo $this->pageCount; ?></span>
            </a>
        <?php endif;?>
        <?php if (isset($this->next)): ?>
            <a href="<?php echo $this->url(array('page' => $this->next)); ?>">
                <span><?php echo 'next'; ?> &gt;</span>
            </a>
        <?php else: ?>
            <span class="disabled"><?php echo 'next'; ?> &gt;</span>
        <?php endif; ?>
    </div>
<?php endif; ?>