0

可能重复:
为 foreach() 提供的参数无效

我的控制行动是

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'); ?>

它显示错误表和页面链接如下 First | < 上一页 | 下一个 > | 最后的

和警告警告:为 C:\Users\Administrator\hello\application\views\scripts\test\index.phtml 中的 foreach() 提供的参数无效为什么会发生这种情况????

4

2 回答 2

0

如果它实现了接口迭代器,你只将数组以外的任何变量传递给 foreach ,我认为 Zend_Paginator 没有实现它。并且您正在使用分页器列出它是完全无法忍受的,只是试试这个

$this->view->records = $db->select()->from('tableviewdemo')->fetchAll()->toArray();

并在您的视图文件中迭代

foreach($this->records)
{
}

我也怀疑您的数据库查询是否有效。

于 2012-10-04T10:52:51.290 回答
0

我注意到你设置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_DbTableZend_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)))
                        ?>">
                            &lt; First</a>
                    <?php else : ?>
                        <span class="disabled">&lt; 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)))
                        ?>">
                            &lt; Prev</a>
                    <?php else : ?>
                        <span class="disabled">&lt; 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 &gt;</a>
                    <?php else : ?>
                        <span class="disabled">Next &gt;</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 &gt;</a>
                    <?php else: ?>
                        <span class="disabled">last &gt;</span>
                    <?php endif ?>
                </td>
            </tr>
        </table>
    </div>
<?php endif; ?>

我希望这有帮助。

于 2012-10-05T10:48:18.457 回答