我注意到你设置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;
}
关于适配器的说明。
适配器之间最大的明显区别在于DbTable返回一个数组将使用数组表示法,而DbTableSelect返回一个对象(行集对象)使用对象表示法“$item->name”,因此请使用适合您需要的适配器。Zend_Paginator_Adapter_DbTable
Zend_Paginator_Adapter_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
):
<?php
echo $this->paginationControl(
//remember the third parameter is the controls partial
$this->paginator, 'Sliding', '_paginatorControl.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; ?>
我希望这有帮助。