我对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; }
这是我的控制器中的代码
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)); }
下面是构建分页器的视图,
pageCount): ?>previous)): ?> < Previous | < Previous | pagesInRange as $page): ?> current): ?> = $page; ?> | = $page; ?> | next)): ?> Next > Next >
最后代码构成了我的观点
Latest News
paginator)): ?>paginator as $item): ?> $v) { echo "
= $this->paginationControl($this->paginator, 'Sliding', '/latestnews/partial_pagination_control.phtml'); ?>- " . $v['title'] . "
"; } ?>
我会很乐意为你提供任何帮助.
谢谢
西科
$ paginator应设置为4,因为您希望一次只显示4条记录:
$paginator->setItemCountPerPage(4);