Php zend分页中出现意外警告
可能重复:Php zend分页中出现意外警告,php,zend-framework,Php,Zend Framework,可能重复: 我的控制动作是 public function indexAction(){ Zend_View_Helper_PaginationControl::setDefaultViewPartial('index.phtml'); $params = array('host' =>'localhost', 'username' =>'root', 'password'
我的控制动作是
public function indexAction(){
Zend_View_Helper_PaginationControl::setDefaultViewPartial('index.phtml');
$params = array('host' =>'localhost',
'username' =>'root',
'password' =>'',
'dbname' =>'test'
);
$db = new Zend_Db_Adapter_Pdo_Mysql($params);
$select = $db->select()->from('tableviewdemo');
$paginator = Zend_Paginator::factory($select);
$paginator->setCurrentPageNumber($this->_getParam('page', 1));
$paginator->setItemCountPerPage(10);
$this->view->paginator=$paginator;
}
并且查看文件是
<? echo "<table border=1>";
foreach ($this->paginator as $item) {
echo '<tr><td>' . $item['id'] . '</td>';
echo '<td>' . $item['name'] . '</td>';
echo '<td>' . $item['city'] . '</td>';
echo '<td>' . $item['state'] . '</td>';
echo '<td>' . $item['date'] . '</td>';
echo '<td>' . $item['zip'] . '</td></tr>';}
echo "</table>";
echo $this->paginationControl($this->paginator,'Sliding','/test/index.phtml'); ?>
它显示出错误
表和页面链接如下所示
第一个|<上一个|下一个>|最后一个
和警告
警告:为C:\Users\Administrator\hello\application\views\scripts\test\index.phtml中的foreach()提供的参数无效
为什么会发生这种情况???如果foreach实现了接口迭代器,那么您只能将数组以外的任何变量传递给foreach,我认为Zend_Paginator没有实现它。您正在使用paginator列出它,这是完全不可接受的,请尝试一下
$this->view->records = $db->select()->from('tableviewdemo')->fetchAll()->toArray();
并在视图文件中进行迭代
foreach($this->records)
{
}
我还怀疑您的db查询是否正常工作。我注意到您将
index.phtml
设置为默认视图部分,以错误引用报价。。。我不认为那种方法有你认为的效果
至少在一开始,我建议你详细说明一切。指定select(),指定适配器,创建新的分页器。以后你可以走捷径
public function indexAction(){
$params = array('host' =>'localhost',
'username' =>'root',
'password' =>'',
'dbname' =>'test'
);
$db = new Zend_Db_Adapter_Pdo_Mysql($params);
$select = $db->select()->from('tableviewdemo');
//specify paginator adapter
$adapter = new Zend_Paginator_Adapter_DbTableSelect($select);
//instantiate the paginator
$paginator = new Zend_Paginator($adapter);
//if you really have to use the factory, don't forget the string for the adapter.
//The paginator should work without the string, but may not work as expected.
//$paginator = Zend_Paginator::factory($select, 'DbTableSelect');
$paginator->setCurrentPageNumber($this->_getParam('page', 1));
$paginator->setItemCountPerPage(10);
//assign paginator to the view
$this->view->paginator=$paginator;
}
适配器的注意事项。
适配器Zend\u Paginator\u Adapter\u DbTable
和Zend\u Paginator\u Adapter\u DbTableSelect
之间最大的明显区别是DbTable返回一个数组,它将使用数组符号$item['name']
和DbTableSelect返回一个对象(行集对象)使用对象表示法“$item->name”,因此使用适合您需要的适配器
回到我原来的观点。默认的部分视图不引用要显示的页面。它指的是包含分页器控件的局部视图。Zend没有分页控件的默认实现,而只是一个
在你看来试试这个
<!-- assumes you using DbTableSelect paginator adapter -->
<!-- for DbSelect adapter just change to array notation: $item['id'] -->
<table border=1>
<?php foreach ($this->paginator as $item) : ?>
<tr>
<td><?php echo $this->escape($item->id) ?></td>
<td><?php echo $this->escape($item->name) ?></td>
<td><?php echo $this->escape($item->city) ?></td>
<td><?php echo $this->escape($item->state) ?></td>
<td><?php echo $this->escape($item->date) ?></td>
<td><?php echo $this->escape($item->zip) ?></td>
</tr>
</table>
<?php endForeach ?>
<?php echo $this->paginationControl($this->paginator,'Sliding','MyPaginatorControl.phtml'); ?>
以防有人需要,这是我的基本控制:
//views/scripts/_paginatorControl.phtml. Yes. I just copy this file to where ever I need it.
<?php
if ($this->pageCount) :
//you need to add each of the request parameters to url
$params = Zend_Controller_Front::getInstance()
->getRequest()->getParams();
//remove the system parameters
unset($params['module']);
unset($params['controller']);
unset($params['action']);
?>
<div class="paginationControl">
<table>
<tr>
<td>
<!--First Page Link -->
<?php if (isset($this->first)): ?>
<a href="<?php
echo $this->url(array_merge($params, array('page' => $this->first)))
?>">
< First</a>
<?php else : ?>
<span class="disabled">< First</span>
<?php endif ?>
</td>
<td class="space">|</td>
<td>
<!--Previous Page Links-->
<?php if (isset($this->previous)) : ?>
<a href="<?php
echo $this->url(array_merge($params, array('page' => $this->previous)))
?>">
< Prev</a>
<?php else : ?>
<span class="disabled">< Prev</span>
<?php endif ?>
</td>
<td>|
<!--Number page links-->
<?php foreach ($this->pagesInRange as $page): ?>
<?php if ($page != $this->current) : ?>
<a href="<?php
echo $this->url(array_merge($params, array('page' => $page)))
?>">
<?php echo $page ?></a> |
<?php else: ?>
<?php echo $page ?> |
<?php
endif;
endforeach;
?>
</td>
<td>
<!--Next page link-->
<?php if (isset($this->next)) : ?>
<a href="<?php
echo $this->url(array_merge($params, array('page' => $this->next)))
?>">
Next ></a>
<?php else : ?>
<span class="disabled">Next ></span>
<?php endif; ?>
</td>
<td class="space">|</td>
<td>
<!--Last page Link -->
<?php if (isset($this->last)): ?>
<a href="<?php
echo $this->url(array_merge($params, array('page' => $this->last)))
?>">
Last ></a>
<?php else: ?>
<span class="disabled">last ></span>
<?php endif ?>
</td>
</tr>
</table>
</div>
<?php endif; ?>
//views/scripts/_paginatorControl.phtml。对我只是把这个文件复制到我需要的地方。
弗斯特
|
上
|
|
|
下一个
|
最后
我希望这会有所帮助。
var\u dump($this->paginator)
,结果是什么?对象(Zend\u paginator)[48]受保护的“\u cacheEnabled”=>布尔值真正受保护的“\u adapter”=>db查询工作正常,我已经完成了你的工作…那么我如何对我的表进行分页…你已经对paginator echo$this->paginationControl进行了分页($this->paginator、'slideing'、'/test/index.phtml')?>进入test/index.phtml,看看那里发生了什么,删除分页器的foreach你已经添加了dif i remove foreach,然后它只显示分页器链接…给出…致命错误:达到了最大函数嵌套级别“100”,Zend_paginator实现了可数和迭代器聚合,所以foreach工作得很好。
//views/scripts/_paginatorControl.phtml. Yes. I just copy this file to where ever I need it.
<?php
if ($this->pageCount) :
//you need to add each of the request parameters to url
$params = Zend_Controller_Front::getInstance()
->getRequest()->getParams();
//remove the system parameters
unset($params['module']);
unset($params['controller']);
unset($params['action']);
?>
<div class="paginationControl">
<table>
<tr>
<td>
<!--First Page Link -->
<?php if (isset($this->first)): ?>
<a href="<?php
echo $this->url(array_merge($params, array('page' => $this->first)))
?>">
< First</a>
<?php else : ?>
<span class="disabled">< First</span>
<?php endif ?>
</td>
<td class="space">|</td>
<td>
<!--Previous Page Links-->
<?php if (isset($this->previous)) : ?>
<a href="<?php
echo $this->url(array_merge($params, array('page' => $this->previous)))
?>">
< Prev</a>
<?php else : ?>
<span class="disabled">< Prev</span>
<?php endif ?>
</td>
<td>|
<!--Number page links-->
<?php foreach ($this->pagesInRange as $page): ?>
<?php if ($page != $this->current) : ?>
<a href="<?php
echo $this->url(array_merge($params, array('page' => $page)))
?>">
<?php echo $page ?></a> |
<?php else: ?>
<?php echo $page ?> |
<?php
endif;
endforeach;
?>
</td>
<td>
<!--Next page link-->
<?php if (isset($this->next)) : ?>
<a href="<?php
echo $this->url(array_merge($params, array('page' => $this->next)))
?>">
Next ></a>
<?php else : ?>
<span class="disabled">Next ></span>
<?php endif; ?>
</td>
<td class="space">|</td>
<td>
<!--Last page Link -->
<?php if (isset($this->last)): ?>
<a href="<?php
echo $this->url(array_merge($params, array('page' => $this->last)))
?>">
Last ></a>
<?php else: ?>
<span class="disabled">last ></span>
<?php endif ?>
</td>
</tr>
</table>
</div>
<?php endif; ?>