Zend Pagination example

12 Nov

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 November 13, 2008 at 3:17 pm #

    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 January 7, 2009 at 1:32 am #

    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 January 15, 2009 at 3:46 am #

    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 January 15, 2009 at 6:20 am #

    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 January 24, 2009 at 2:25 am #

    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 January 26, 2009 at 7:16 pm #

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

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

  7. Thang February 9, 2009 at 5:05 pm #

    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 February 20, 2009 at 5:24 am #

    @Thang

    How do you retrieve the data from the paginator object?

  9. Thang March 5, 2009 at 4:02 am #

    @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 March 17, 2009 at 5:25 pm #

    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 April 2, 2009 at 6:52 am #

    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 April 2, 2009 at 8:15 am #

    Verify if all your other helpers are working fine.

  13. ED.Tsai May 7, 2009 at 2:23 pm #

    a cool tut, it’ worked

  14. bhaskar May 8, 2009 at 10:42 am #

    I am getting error:

    Start | | End Page of

    please tell me, what is wrong.

  15. lily May 24, 2009 at 5:08 pm #

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

  16. zend dev May 28, 2009 at 6:10 am #

    @vladimir

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

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

  17. Luis June 2, 2009 at 2:31 pm #

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

  18. Mr. G June 15, 2009 at 7:08 am #

    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 June 15, 2009 at 7:23 am #

    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 June 25, 2009 at 9:46 pm #

    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 June 29, 2009 at 8:22 am #

    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 July 8, 2009 at 9:42 am #

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: