通过 Zend Paginator 分页给出错误
我正在我的应用程序中使用 zend paginator 进行分页,这是正在执行的操作
public function listAction(){
$registry = Zend_Registry::getInstance();
$DB = $registry['DB'];
$sql = "SELECT * FROM `task` ORDER BY task_name ASC";
$result = $DB->fetchAll($sql);
$page=$this->_getParam('page',1);
$paginator = Zend_Paginator::factory($result);
$paginator->setItemCountPerPage(3);
$paginator->setCurrentPageNumber($page);
$this->view->paginator=$paginator;
}
,这是此操作的视图,
<table border="1" align="center">
<tr>
<th>Task Name</th>
<th>Task Assign To</th>
<th>Action</th>
</tr>
<?php
foreach($this->paginator as $record){ ?>
<tr>
<td><?php echo $record['task_name']?></td>
<td><?php echo $record['task_assign']?></td>
<td>
<a href="edit/id/<?php echo $record['id']?>">Edit</a>
|
<a href="del/id/<?php echo $record['id']?>">Delete</a>
</td>
</tr>
<?php } ?>
</table>
<?php echo $this->paginationControl($this->paginator, 'Sliding', 'pagination.phtml'); ?>
这样做会给我这个错误,
Fatal error: Cannot use object of type stdClass as array in C:\xampp\htdocs\zend_login\application\views\scripts\task\list.phtml on line 47
第 47 行指的是
<td><?php echo $record['task_name']?></td>
使用此函数将对象转换为数组的情况
function objectToArray( $object ){
if( !is_object( $object ) && !is_array( $object ) ){
return $object;}
if( is_object( $object ) ){
$object = get_object_vars( $object );}
return array_map( 'objectToArray', $object );}
$paginator = objectToArray($this->paginator);
我现在 在我的代码中更改此内容
foreach($paginator as $record){
,也更改此内容
<?php echo $this->paginationControl($paginator, 'Sliding', 'pagination.phtml'); ?>
,但它现在给了我这个错误,
Catchable fatal error: Argument 1 passed to Zend_View_Helper_PaginationControl::paginationControl() must be an instance of Zend_Paginator, array given in C:\xampp\htdocs\zend_login\library\Zend\View\Helper\PaginationControl.php on line 88
你能告诉我我缺少什么吗?我是 zend 的新手,所以请不要介意我尴尬的问题 这是我的ErrorController.php
class ErrorController extends Zend_Controller_Action{
public function errorAction()
{
$errors = $this->_getParam('error_handler');
switch ($errors->type) {
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
// 404 error -- controller or action not found
$this->getResponse()->setHttpResponseCode(404);
$this->view->message = 'Page not found';
break;
default:
// application error
$this->getResponse()->setHttpResponseCode(500);
$this->view->message = 'Application error';
break;
}
// Log exception, if logger available
if ($log = $this->getLog()) {
$log->crit($this->view->message, $errors->exception);
}
// conditionally display exceptions
if ($this->getInvokeArg('displayExceptions') == true) {
$this->view->exception = $errors->exception;
}
$this->view->request = $errors->request;
}
public function getLog()
{
$bootstrap = $this->getInvokeArg('bootstrap');
if (!$bootstrap->hasPluginResource('Log')) {
return false;
}
$log = $bootstrap->getResource('Log');
return $log;
}
}
i am using zend paginator for pagination in my app here is the action that is doing so
public function listAction(){
$registry = Zend_Registry::getInstance();
$DB = $registry['DB'];
$sql = "SELECT * FROM `task` ORDER BY task_name ASC";
$result = $DB->fetchAll($sql);
$page=$this->_getParam('page',1);
$paginator = Zend_Paginator::factory($result);
$paginator->setItemCountPerPage(3);
$paginator->setCurrentPageNumber($page);
$this->view->paginator=$paginator;
}
and this is the view for this action
<table border="1" align="center">
<tr>
<th>Task Name</th>
<th>Task Assign To</th>
<th>Action</th>
</tr>
<?php
foreach($this->paginator as $record){ ?>
<tr>
<td><?php echo $record['task_name']?></td>
<td><?php echo $record['task_assign']?></td>
<td>
<a href="edit/id/<?php echo $record['id']?>">Edit</a>
|
<a href="del/id/<?php echo $record['id']?>">Delete</a>
</td>
</tr>
<?php } ?>
</table>
<?php echo $this->paginationControl($this->paginator, 'Sliding', 'pagination.phtml'); ?>
by doing so it giving me this error
Fatal error: Cannot use object of type stdClass as array in C:\xampp\htdocs\zend_login\application\views\scripts\task\list.phtml on line 47
line 47 refers to
<td><?php echo $record['task_name']?></td>
than i used this function to convert object to array
function objectToArray( $object ){
if( !is_object( $object ) && !is_array( $object ) ){
return $object;}
if( is_object( $object ) ){
$object = get_object_vars( $object );}
return array_map( 'objectToArray', $object );}
$paginator = objectToArray($this->paginator);
now i change this in my code
foreach($paginator as $record){
and also this
<?php echo $this->paginationControl($paginator, 'Sliding', 'pagination.phtml'); ?>
but its giving me this error now
Catchable fatal error: Argument 1 passed to Zend_View_Helper_PaginationControl::paginationControl() must be an instance of Zend_Paginator, array given in C:\xampp\htdocs\zend_login\library\Zend\View\Helper\PaginationControl.php on line 88
can you please tell what i am missing i am new to zend so please dont mind my awkward question
this is my ErrorController.php
class ErrorController extends Zend_Controller_Action{
public function errorAction()
{
$errors = $this->_getParam('error_handler');
switch ($errors->type) {
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
// 404 error -- controller or action not found
$this->getResponse()->setHttpResponseCode(404);
$this->view->message = 'Page not found';
break;
default:
// application error
$this->getResponse()->setHttpResponseCode(500);
$this->view->message = 'Application error';
break;
}
// Log exception, if logger available
if ($log = $this->getLog()) {
$log->crit($this->view->message, $errors->exception);
}
// conditionally display exceptions
if ($this->getInvokeArg('displayExceptions') == true) {
$this->view->exception = $errors->exception;
}
$this->view->request = $errors->request;
}
public function getLog()
{
$bootstrap = $this->getInvokeArg('bootstrap');
if (!$bootstrap->hasPluginResource('Log')) {
return false;
}
$log = $bootstrap->getResource('Log');
return $log;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只需将其作为对象调用:
更新:聊天中解决的所有其他错误
You just need to call it as an object:
UPDATE: all other errors resolved in chat