Zend Pagination - 似乎不限制每页的结果数量

1 php zend-framework zend-paginator

我对The Zend框架相对较新,他最多只使用了两个月.我现在试图从我运行的查询中获取结果列表,通过zend paginator类发送它并每页显示4个结果,但是它似乎没有识别它何时有4个结果,我相信有一个我的代码中的错误,但我不能为我的生活看到它,我希望有人在这里能够接受它并帮助我和我可能会告诉我这是我错过的愚蠢.谢谢,这是我写的代码.

这是我模型中的查询

public function getAllNews()
{
    $db = Zend_Registry::get('db');

    $sql = "SELECT * FROM $this->_name WHERE flag = 1 ORDER BY created_at";

    $query = $db->query($sql);
    while ($row = $query->fetchAll()) {
        $result[] = $row;
    }

    return $result;
}
Run Code Online (Sandbox Code Playgroud)

这是我的控制器中的代码

function preDispatch()
{
$this->_helper->layout->setLayout('latestnews');
    Zend_Loader::loadClass('newsArticles');
    $allNews = new newsArticles();
    $this->view->allNews = $allNews->getAllnews();
    //die (var_dump($this->view->allNews));
    $data = $this->view->allNews;
    // Instantiate the Zend Paginator and give it the Zend_Db_Select instance  Argument ($selection)
    $paginator = Zend_Paginator::factory($data);
    // Set parameters for paginator
    $paginator->setCurrentPageNumber($this->_getParam("page")); // Note: For this to work of course, your URL must be something like this: http://localhost:8888/index/index/page/1  <- meaning we are currently on page one, and pass that value into the "setCurrentPageNumber"
    $paginator->setItemCountPerPage(1);
    $paginator->setPageRange(4);
    // Make paginator available in your views
    $this->view->paginator = $paginator;
    //die(var_dump($data));
}
Run Code Online (Sandbox Code Playgroud)

下面是构建分页器的视图,

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

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

<!-- Next page link --> 
<?php if (isset($this->next)): ?> 
  <a href="<?= $this->url(array('page' => $this->next)); ?>">Next &gt;</a>
<?php else: ?> 
  <span class="disabled">Next &gt;</span>
<?php endif; ?> 
</div> 
<?php endif; ?> 
Run Code Online (Sandbox Code Playgroud)

最后代码构成了我的观点

<div id="rightColumn">
                <h3>Latest News</h3>
                <?php if (count($this->paginator)): ?>
                    <ul>
                        <?php foreach ($this->paginator as $item): ?>
                            <?php
                            foreach ($item as $k => $v)
                            {
                                echo "<li>" . $v['title'] . "</li>";
                            }
                            ?>

                        <?php endforeach; ?>
                    </ul>
                <?php endif; ?>

<?= $this->paginationControl($this->paginator, 'Sliding', '/latestnews/partial_pagination_control.phtml'); ?> 
                </div>
Run Code Online (Sandbox Code Playgroud)

我会很乐意为你提供任何帮助.

谢谢

西科

Pus*_*dra 8

$ paginator应设置为4,因为您希望一次只显示4条记录:

$paginator->setItemCountPerPage(4);
Run Code Online (Sandbox Code Playgroud)