When I was trying to use pagination I didnot find a complete example . From different blogs and mannuals I finally figured it . So I thought of posting it for myself and for others trying to learn it .
You need to grab the result and pass to Zend_Paginator .
/*
* Get the page number , default 1
*/
$page = $this->_getParam('page',1);
/*
* Object of Zend_Paginator
*/
$paginator = Zend_Paginator::factory($result);
/*
* Set the number of counts in a page
*/
$paginator->setItemCountPerPage(2);
/*
* Set the current page number
*/
$paginator->setCurrentPageNumber($page);
/*
* Assign to view
*/
$this->view->paginator = $paginator;
I hope you have this code in the indexAction . If its in indexAction add the below code in the index.phtml view script , else according to the view of your Action .
<h1>Example</h1> <?php if (count($this->paginator)): ?> <ul> <?php /* * Iterate through the paginator to show the results */ foreach ($this->paginator as $item): ?> <li><?php echo $item['FirstName'] ." ".$item['LastName']; ?></li> <?php endforeach; ?> </ul> <?php endif; ?> <?php /* * Print the pagination of type , drop down or search type or of your choice. */ echo $this->paginationControl($this->paginator, 'Sliding', '/partials/my_pagination_control.phtml'); ?>
This code is from the zend mannuals , but it didnot tell where to keep this . You need to keep this in the views folder . I am keeping it in application/views/scripts/partials/my_pagination_control.phtml . I have added the 3 types of pagination in this . You can remove or comment it and look how each type of pagination looks . The drop down is uncommented for now .
<?php
/*
* Search pagination
*/
?>
<?php /*if ($this->pageCount): ?>
<div class="paginationControl">
<!-- Previous page link -->
<?php if (isset($this->previous)): ?>
<a href="<?php echo $this->url(array('page' => $this->previous)); ?>"> << Previous</a> |
<?php else: ?>
<span class="disabled">< Previous</span> |
<?php endif; ?>
<!-- Numbered page links -->
<?php foreach ($this->pagesInRange as $page): ?>
<?php if ($page != $this->current): ?>
<a href="<?php echo $this->url(array('page' => $page)); ?>">
<?php echo $page; ?>
</a> |
<?php else: ?>
<?php echo $page; ?> |
<?php endif; ?>
<?php endforeach; ?>
<!-- Next page link -->
<?php if (isset($this->next)): ?>
<a href="<?php echo $this->url(array('page' => $this->next)); ?>">
Next >
</a>
<?php else: ?>
<span class="disabled">Next ></span>
<?php endif; ?>
</div>
<?php endif; */?>
<?php
/*
* Item pagination example
*
*/
?>
<!--
See http://developer.yahoo.com/ypatterns/pattern.php?pattern=itempagination
-->
<?php /*if ($this->pageCount): ?>
<div class="paginationControl">
<?php echo $this->firstItemNumber; ?> - <?php echo $this->lastItemNumber; ?>
of <?php echo $this->totalItemCount; ?>
<!-- First page link -->
<?php if (isset($this->previous)): ?>
<a href="<?php echo $this->url(array('page' => $this->first)); ?>">First </a> |
<?php else: ?>
<span class="disabled">First</span> |
<?php endif; ?>
<!-- Previous page link -->
<?php if (isset($this->previous)): ?>
<a href="<?php echo $this->url(array('page' => $this->previous)); ?>"> < Previous </a> |
<?php else: ?>
<span class="disabled">< Previous</span> |
<?php endif; ?>
<!-- Next page link -->
<?php if (isset($this->next)): ?>
<a href="<?php echo $this->url(array('page' => $this->next)); ?>"> Next > </a> |
<?php else: ?>
<span class="disabled">Next ></span> |
<?php endif; ?>
<!-- Last page link -->
<?php if (isset($this->next)): ?>
<a href="<?php echo $this->url(array('page' => $this->last)); ?>"> Last </a>
<?php else: ?>
<span class="disabled">Last</span>
<?php endif; ?>
</div>
<?php endif; */?>
<?php
/*
* Drop down paginaton example
*/
?>
<?php if ($this->pageCount): ?>
<select id="paginationControl" size="1">
<?php foreach ($this->pagesInRange as $page): ?>
<?php $selected = ($page == $this->current) ? ' selected="selected"' : ''; ?>
<option value="<?php
echo $this->url(array('page' => $page));?>"<?php echo $selected ?>>
<?php echo $page; ?>
</option>
<?php endforeach; ?>
</select>
<?php endif; ?>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.2/prototype.js"></script>
<script type="text/javascript">
$('paginationControl').observe('change', function() {
window.location = this.options[this.selectedIndex].value;
})
</script>Hope it works for you . Thanks


Comments
Complete Working Code
The complete working code is posted in github . Feel free to grab the code http://github.com/harikt/zfpagination and don't be shy as Jon Lebanord of zend casts tells .
Nice Article, Thanks for
Nice Article, Thanks for posting
Merci, c'est simple ,
Merci, c'est simple , compréhensif, et clair
bravo
Я не могу скачать полностью
Я не могу скачать полностью скрипт. Обновите ссылку
If it doesn't works
If you have any trouble downloading it , consider installing git and get the files from command line as
$ git clone git://github.com/harikt/zfpagination.git
Hope it helps you . Look git for more information .
Thank you
Thank you for this post,
Zend Framework community need more wiki like this !
Nice work
Thank you for your comments .
Thank you for your comments . I am happy to hear that it helps some .
Thanks once again .
Hi, I see the example code
Hi,
I see the example code and files you are given.
But there is no download link I found?
I am using zend framework with mvc based structure as mentioned on zend.com but I dont get any perfect example of paging in mvc structure.
Please look more closely
Please look more closely in github. You can download it .
Thank U very much.Its great
Thank U very much.Its great help for me
nice, thank you very much!
nice, thank you very much!
Thanks a LOT for this, it's
Thanks a LOT for this, it's got to be one of the most straightforward tutorials I've read in a while.
Great job, keep it up!
Thanks for this post
than x for putting this post i search many site but i got complete pagination but still it display page even page is not there. how can i know page which is more than 1.
SWEET
Works PERFECT! Thank you so much for keeping me from possible countless hours of research!
Ajax Zend Pagination
Could you give me the implementation of Ajax zend pagination? I have a problem with the running time of fetching data from db. It almost takes 6 - 10 second for each page.
Thanks a lot for this post
Great work is done here do u have ajax pagination in zend.
Nice post
Congratulations! nice post and thanks for share.
zend pagination
Thanks for this wonderful tutorial. I would be able to set up pagination in "zend" way. You ROCKZ....
Save time
Hi . it's save my development time a lot. Thanks for this post.
It's very helpful for the new
It's very helpful for the new bee..
nice code, Thanks
nice code, Thanks
Add new comment