Zend Pagination example


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.
Lets have a simple example.
First create a template file pagination.phtml in your “/application/view/script/” folder and place the following code

<div style="width:100%">
    <div style="float:left;width:28%">
    </div>
    <div style="float:right;width:70%;">
        <!-- First page link -->
        <?php if (isset($this->previous)): ?>
              <a href="<?= $this->url(array('page' => $this->first)); ?>">Start</a> |
        <?php else: ?>
                <span>Start</span> |
        <?php endif; ?>
    
        <!-- Previous page link -->
    
        <?php if (isset($this->previous)): ?>
              <a href="<?= $this->url(array('page' => $this->previous)); ?>">&lt; Previous</a> |
        <?php else: ?>
            <span>&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>Next &gt;</span> |
        <?php endif; ?>
        <!-- Last page link -->
        <?php if (isset($this->next)): ?>
              <a href="<?= $this->url(array('page' => $this->last)); ?>">End</a>
        <?php else: ?>
            <span>End</span>
        <?php endif; ?>
        &nbsp; Page <?= $this->current; ?> of <?= $this->last; ?>
    </div>
 </div>

The code above is self explanatory. We are creating links for the pagination.

Next add the following code in your controller.

$sql = 'SELECT * FROM table_name ';
    $result = $db->fetchAll($sql);
    $page=$this->_getParam('page',1);
    $paginator = Zend_Paginator::factory($result);
    $paginator->setItemCountPerPage(10));
    $paginator->setCurrentPageNumber($page);

    $this->view->paginator=$paginator;

In the code above we first fetch records from our database. Then instantiate our paginator by writing

$paginator = Zend_Paginator::factory($result);

And pass it results we have fatched.
We then set number of items per page and current page. On our first visit $page will have value 1.
At the end we assign this paginator to our view template.
And now in view template add the following code

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

    <?= $this->paginationControl($this->paginator, 'Sliding', 'pagination.phtml'); ?

We are applying foreach loop to our paginator and echo our records. I assume that you have fistname, lastname columns in your table where you are fetching records form.Last line is compulsory. you need to call the paginator contorller and give it the $paginator, style of the pagination and the file name of the pagination template.

From zendgeek.blogspot.com

Advertisements

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