Zend Framework

Zend Pagination example

Posted by Faheem Abbas on November 12, 2008

Paginator component is available with Zend Framework v1.6. This component wasn’t available in v1.5. I appreciate Zend for provide such a nice component for pagination. This component, like other component, is so loosely coupled that you can use it wherever you want without worrying about any other component at all.
If you have already created pages and you want to apply pagination to them, it would not be a big deal.
Pagination is three step process.

1. you will need to create template file. In template file you can specify layout of your pagination. i.e. how your first, previous, next and last etc will be displayed.

2. instantiate your pagination class in the controller and pass data source- data source can be an array, values fetched form the database etc.

3. make some change in your template file, where you are showing your records.

“Read full article and example here” http://zendgeek.blogspot.com/2009/07/zend-pagination-example.html

22 Responses to “Zend Pagination example”

  1. Vladimir said

    Thank you for this tutorial it is very nice and pretty much easy to understand. I did all by example, but when i get results, i have limited them by 10, but paging links wont work :/
    I mean, when i clik on link, nothing happens.

  2. Slavi said

    If you have 100k rows your script will die because of these

    $sql = ‘SELECT * FROM table_name ‘;
    $result = $db->fetchAll($sql);

    for 1and1 you can SELECT up to 1000 rows without Internal Server Error

  3. Jaimie said

    This is horrible way to do things. I have tables with over 11 million rows hmm wonder how this poor example will work.

  4. Faheem Abbas said

    Dear Jaimie,
    This article is written on how to use zend framework paginator component, so i didn’t consider any performance issues.
    Its up to you how you handle fetching and improve your performance.

  5. hamid said

    Well,

    I have a question, If my select is dynamic posted from form, so how to set a pagination in this case, i need to know how I post again the parameter of my first post with pagination link ?

  6. Milan said

    Yes Hamid – I just wanted to ask the same question.

    So how we should handle paginator + filter form using post method?

  7. Thang said

    For whom facing the performance problem:

    It’s unnecessary to perform fectchAll. It may lead to time out issue in large database.
    Just use a SQL as an adapter (not the result) like this:

    $select = $dbAdapter->select()
    ->from(array(‘j’ => ‘my_system’), array(‘id’, ‘news’))
    ->order(‘id’);
    $this->view->paginator = Zend_Paginator::factory($select);

  8. Derek Reynolds said

    @Thang

    How do you retrieve the data from the paginator object?

  9. Thang said

    @Derek Reynolds:
    You can use code posted by author of this entry:

    foreach($this->paginator as $record)
    {
    echo $record[‘firstname’].’\n’;
    echo $record[‘lastname’]. ‘\n’


    }

    paginationControl($this->paginator, ‘Sliding’, ‘pagination.phtml’); ?>

    What I wondering is pagination in AJAX with ZF. Should we use Zend_paginator or Dojo Pagination? How can we do it?

  10. Merthin said

    Thanks for this example, it really helped to me to understand the pagination. The examples in zf documentation are a little bit complicated (for me at least)

    Cheers

  11. Aswani Kumar said

    Hi

    I am facing the problem in the following line:

    paginationControl($this->paginator, ‘Sliding’, ‘pagination.phtml’); ?>

    problem is: exception ‘Zend_View_Exception’ with message ‘helper ‘PaginationControl’ not found in path’ in /var/www/html/vpe/district/Zend/View/Abstract.php:1004

    Please clarify it…. i m stuck now…

  12. Faheem Abbas said

    Verify if all your other helpers are working fine.

  13. ED.Tsai said

    a cool tut, it’ worked

  14. bhaskar said

    I am getting error:

    Start | | End Page of

    please tell me, what is wrong.

  15. lily said

    Thank you so much…your tutorial is so great!!!

  16. zend dev said

    @vladimir

    in controller
    instead of writing this
    $paginator->setCurrentPageNumber($page);

    write this
    $paginator->setCurrentPageNumber($this->_request->getParam(‘page’));

  17. Luis said

    Great job in explaining the Zend pagination. It really helps.

  18. Mr. G said

    Hello,
    You are writing great tutorials.

    I have a problem with this line:
    paginationControl($this->paginator, ‘Sliding’, ‘pagination.phtml’); ?>

    In my view it looks like:
    paginationControl($this->paginator, ‘Sliding’, ‘pagination.phtml’); ?>

    And I don’t have any page’s links.

  19. Mr. G said

    ok sorry I fixed it.

    instead of this:
    paginationControl($this->paginator, ‘Sliding’, ‘pagination.phtml’); ?>
    I wrote this:
    paginationControl($this->paginator, ‘Sliding’, ‘pagination.phtml’); ?>

    But now I have a error when clicking on next:
    Fatal error: Uncaught exception 'Zend_Controller_Action_Exception' with message 'Action "3c" does not exist and was not trapped in __call()'

  20. ayesha said

    thanks, other then select* thing, this example works, and I am thankful for saving my time that I would have spent on writing my pagination.html…I think I am going to implement dojo pagination eventually but for now this is working…

  21. Brahmam said

    Thanks for your Zend articles and examples more helping me. I am fresher for zend framework in few days before. But now i am gaining good experiance for your articles.

  22. shashi said

    Can any one give a clear note about it…and this programming is not working for me

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>