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

发布于 2024-07-10 20:05:01 字数 3550 浏览 10 评论 0原文

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

这是我的控制器中的代码

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>

我将非常感谢您能给我的任何帮助。

谢谢西科

I am relatively new to The Zend framework having only been working with it probably two months at the most. I am now trying to get a list of results from a query I run, send it through the zend paginator class and display 4 results per page, however it does not seem to be recognising when it has got 4 results, I believe there is an error in my code but I can not for the life of me see it, I was hoping someone here will be able to pick up on it and help me and out and probably tell me it is something stupid that I have missed. Thanks here is the code I have written.

This the query in my model

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

This is code in my controller

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

Below is the view that builds the paginator,

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

And finally the code the makes up my view

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

I will be greatful for any help you can give me.

Thanks

Sico

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迷迭香的记忆 2024-07-17 20:05:01

$paginator 应设置为 4,因为您想一次仅显示 4 条记录:

$paginator->setItemCountPerPage(4);

$paginator should be set to 4 as you want to show only 4 records at a time :

$paginator->setItemCountPerPage(4);
荒芜了季节 2024-07-17 20:05:01

所以我认为问题的根源在于带有 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);
}

So I think the root of your problem is in your model class with the getAllNews function. The Zend_Db fetchAll function retrieves an associative array of all the records matching your query. You are returning an array of one element that is an array of all the rows retrieved by your select statement.

Try this:

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

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

        return $db->fetchAll($sql);
}
沒落の蓅哖 2024-07-17 20:05:01

如果您希望分页器限制您的查询,则不应将值传递给分页器。
有一个代码适合您

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

You sould not pass values to paginator if you want it to limit your query.
There is a code will work for you

$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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文