1

我对 Zend 框架比较陌生,最多只使用它两个月。我现在正试图从我运行的查询中获取结果列表,通过 zend 分页器类发送它并每页显示 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;
}

这是我的控制器中的代码

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

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

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

最后是构成我观点的代码

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

我会很感激你能给我的任何帮助。

谢谢

西科

4

3 回答 3

8

$paginator 应该设置为 4 因为你想一次只显示 4 条记录:

$paginator->setItemCountPerPage(4);
于 2011-05-04T11:08:38.890 回答
1

如果您希望它限制您的查询,您不应该将值传递给分页器。有一个代码会为你工作

$usersTable = $this->getTable();
        $registerSelect = $usersTable->select()->where('status = ?', 'OK')->order('lastLogin DESC');
        $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbTableSelect($registerSelect));
        $paginator->setCurrentPageNumber($page);
        $paginator->setItemCountPerPage($limit);
于 2009-01-30T01:14:44.067 回答
1

所以我认为你的问题的根源在于你的带有 getAllNews 函数的模型类。Zend_Db fetchAll 函数检索与查询匹配的所有记录的关联数组。您将返回一个包含一个元素的数组,该元素是您的 select 语句检索到的所有行的数组。

尝试这个:

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

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

        return $db->fetchAll($sql);
}
于 2009-01-01T18:23:21.287 回答