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

次のようにエラーテーブルとページリンクが表示されています。< 前へ | 次へ > | 最後

および警告 警告: C:\Users\Administrator\hello\application\views\scripts\test\index.phtml の foreach() に無効な引数が指定されました なぜこれが起こるのですか????

4

2 に答える 2

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が配列表記を使用してarayを返し、 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 に答える
0

インターフェイスイテレータを実装している場合は、配列以外の変数のみをforeachに渡します。Zend_Paginatorはそれを実装していないと思います。そしてあなたはそれをリストするためにpaginatorを使用していますそれは完全に実行不可能ですこれを試してみてください

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

ビューファイルで繰り返します

foreach($this->records)
{
}

また、dbクエリが機能しているかどうかも疑問です。

于 2012-10-04T10:52:51.290 に答える