views:

1606

answers:

2

Hello,

I am relatively new to The Zend framework having only been working with it probably two months at the most. I am now trying to get a list of results from a query I run, send it through the zend paginator class and display 4 results per page, however it does not seem to be recognising when it has got 4 results, I believe there is an error in my code but I can not for the life of me see it, I was hoping someone here will be able to pick up on it and help me and out and probably tell me it is something stupid that I have missed. Thanks here is the code I have written.

This the query in my model

public function getAllNews()
{
 $db = Zend_Registry::get('db');

 $sql = "SELECT * FROM $this->_name WHERE flag = 1 ORDER BY created_at";

 $query = $db->query($sql);
 while ($row = $query->fetchAll()) {
     $result[] = $row;
 }

 return $result;
}

This is code in my controller

function preDispatch()
{
$this->_helper->layout->setLayout('latestnews');
 Zend_Loader::loadClass('newsArticles');
 $allNews = new newsArticles();
 $this->view->allNews = $allNews->getAllnews();
 //die (var_dump($this->view->allNews));
 $data = $this->view->allNews;
 // Instantiate the Zend Paginator and give it the Zend_Db_Select instance  Argument ($selection)
    $paginator = Zend_Paginator::factory($data);
 // Set parameters for paginator
    $paginator->setCurrentPageNumber($this->_getParam("page")); // Note: For this to work of course, your URL must be something like this: http://localhost:8888/index/index/page/1  <- meaning we are currently on page one, and pass that value into the "setCurrentPageNumber"
    $paginator->setItemCountPerPage(1);
    $paginator->setPageRange(4);
 // Make paginator available in your views
    $this->view->paginator = $paginator;
 //die(var_dump($data));
}

Below is the view that builds the paginator,

    <?php if ($this->pageCount): ?> 
<div class="paginationControl">
<!-- Previous page link --> 
<?php if (isset($this->previous)): ?> 
  <a href="<?= $this->url(array('page' => $this->previous)); ?>">&lt; Previous</a> | 
<?php else: ?> 
  <span class="disabled">&lt; Previous</span> | 
<?php endif; ?> 

<!-- Numbered page links -->
<?php foreach ($this->pagesInRange as $page): ?> 
  <?php if ($page != $this->current): ?>
    <a href="<?= $this->url(array('page' => $page)); ?>"><?= $page; ?></a> | 
  <?php else: ?>
    <?= $page; ?> | 
  <?php endif; ?>
<?php endforeach; ?>

<!-- Next page link --> 
<?php if (isset($this->next)): ?> 
  <a href="<?= $this->url(array('page' => $this->next)); ?>">Next &gt;</a>
<?php else: ?> 
  <span class="disabled">Next &gt;</span>
<?php endif; ?> 
</div> 
<?php endif; ?>

And finally the code the makes up my view

<div id="rightColumn">
       <h3>Latest News</h3>
       <?php if (count($this->paginator)): ?>
        <ul>
         <?php foreach ($this->paginator as $item): ?>
          <?php
          foreach ($item as $k => $v)
          {
           echo "<li>" . $v['title'] . "</li>";
          }
          ?>

         <?php endforeach; ?>
        </ul>
       <?php endif; ?>

<?= $this->paginationControl($this->paginator, 'Sliding', '/latestnews/partial_pagination_control.phtml'); ?> 
       </div>

I will be greatful for any help you can give me.

Thanks

Sico

+1  A: 

So I think the root of your problem is in your model class with the getAllNews function. The Zend_Db fetchAll function retrieves an associative array of all the records matching your query. You are returning an array of one element that is an array of all the rows retrieved by your select statement.

Try this:

public function getAllNews()
{
        $db = Zend_Registry::get('db');

        $sql = "SELECT * FROM $this->_name WHERE flag = 1 ORDER BY created_at";

        return $db->fetchAll($sql);
}
Brian Fisher
A: 

You sould not pass values to paginator if you want it to limit your query. There is a code will work for you

$usersTable = $this->getTable();
     $registerSelect = $usersTable->select()->where('status = ?', 'OK')->order('lastLogin DESC');
     $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbTableSelect($registerSelect));
     $paginator->setCurrentPageNumber($page);
     $paginator->setItemCountPerPage($limit);
waney