0

Possible Duplicate:
Invalid argument supplied for foreach()

my controllar action is

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;
}

and view file is

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

it is showing error table and pagelink as following First | < Previous | Next > | Last

and warning Warning: Invalid argument supplied for foreach() in C:\Users\Administrator\hello\application\views\scripts\test\index.phtml why this happening????

Community
  • 1
  • 1
Ammar Hayder Khan
  • 1,287
  • 4
  • 22
  • 49

2 Answers2

0

You only pass any variable except arrays to foreach if it implements the interface iterators , I think Zend_Paginator doesn't implement it . and You are using paginator for listing it is totally unexeptable just try this

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

and iterate in your view file

foreach($this->records)
{
}

I have also doubt that your db query is working or not.

Rupesh Patel
  • 3,015
  • 5
  • 28
  • 49
  • db query is working perfactly, i have already done your thing ...so how can i paginate my table... – Ammar Hayder Khan Oct 04 '12 at 10:58
  • you have already echo the paginator echo $this->paginationControl($this->paginator,'Sliding','/test/index.phtml'); ?> go in test/index.phtml and see what is going on there remove foreach of paginator you have added – Rupesh Patel Oct 04 '12 at 11:00
  • if i remove foreach then it's showing nothing but paginator link.....giving ...Fatal error: Maximum function nesting level of '100' reached, – Ammar Hayder Khan Oct 04 '12 at 11:12
  • Zend_Paginator implements Countable and IteratorAggregate so foreach works just fine. – RockyFord Oct 05 '12 at 10:01
0

I noticed you set index.phtml as your default view partial, to misquote a quote ... I don't think that method does what you think it does.

At least in the beginning I suggest you specify everything. Specify the select(), specify the adapter, create a new paginator. Later you can take the short cuts.

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;
}

A note on adapters.

The biggest apparent difference between the adapters Zend_Paginator_Adapter_DbTable and Zend_Paginator_Adapter_DbTableSelect is that DbTable returns an aray will use the array notation $item['name'] and DbTableSelect returns an object (rowset object) uses object notation `$item->name' so use the adapter appropriate to your needs.

To get back to my original point. The default view partial does not refer the page you want displayed. It refers to the view partial that contains the controls for your paginator. Zend does not have a default implementation of pagination controls just an example.

Try this in your view.

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

The paginator I typically use is very similar to the example in the manual, I call it in my view script (the default location for all partial views is views/scripts) :

<?php
    echo $this->paginationControl(
        //remember the third parameter is the controls partial
        $this->paginator, 'Sliding', '_paginatorControl.phtml'
    )
 ?>

Just in case someone needs it here is my basic control partial:

//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; ?>

I hope this helps.

RockyFord
  • 8,529
  • 1
  • 15
  • 21