Special Offer - Get $90 Discount and Host Your Dream Web Site @ Dreamhost for 1 Year Click Here. Promo Code SLJ345
Back To Top
WordPress Web Application Development
Develop Powerfull Web Applications Using Cutting Edge WordPress Development Techniques

Doctrine Pagination With Codeigniter Framework

on 2011/06/30 4:25 AM stored in: Uncategorized and tagged:

Introduction To Database Pagination Using Doctrine

Pagination is a essential feature in displaying list of items to improve the performance. Normally we get the results from database and prepares pagination links using an external library or default library provided by the framework. But now with the introduction of doctrine pagination utility classes we can directly manage the pagination using the doctrine orm functionality. In this post I’m going to explain how to plug the doctrine pagination functionality to codeigniter project.

Integrating Doctrine Pagination with Codeigniter

Integrating pagination to codeigniter is very simple. The only thing you have to do is create a function inside the controller class and configure the function url inside the doctrine pagination code. Pagination code for doctrine is shown below with the codeigniter function.

 

        function pager($page='1') {

                $currentPage = $page;
                $resultsPerPage = 10;


                $pagerLayout = new Doctrine_Pager_Layout(
                                new Doctrine_Pager(
                                Doctrine_Query::create()
                                ->from( 'Txtcountry u' ),
                                $currentPage, // Current page of request
                                $resultsPerPage
                                ),
                                new Doctrine_Pager_Range_Sliding(array(
                                                'chunk' => 5
                                )),
                                'http://www.example.com/doctrin/pager/{%page_number}'
                );

                $pagerLayout->setTemplate('[{%page}]');
                $pagerLayout->setSelectedTemplate('[{%page}]');

                // Retrieving Doctrine_Pager instance
                $pager = $pagerLayout->getPager();

                // Fetching users
                $users = $pager->execute(); // This is possible too!

                foreach ($users as $key => $value) {
                        echo $value['name']."
"; } $pagerLayout->display(); }

Code Explanation

  • First variable $currentpage contains the page number of the currently displayed page. This number has to be passed as a optional parameter in the codeigniter url.
  • Next variable $resultsPerPage configures the number of results to be displayed in a single page.
  • Then we create a object using Doctrine_Pager_Layout class. This class generates the pagination links according to the given parameters.
  • We need to pass 3 variables to the Doctrine_Pager_Layout object.
  • First Parameter to Doctrine_Pager_Layout is Doctrine_Pager object which needs dql query,current page and results per page as parameters.
  • Second Parameter to Doctrine_Pager_Layout is Doctrine_Pager_Range_Sliding object. I have passed ‘chunk’ => 5 to that object. That means you will get links to 5 pages in the pagination bar.
  • Third Parameter to Doctrine_Pager_Layout is the pagination url. In this example you have to define your hostname followed by controller name followed by function name and then the page number using dynamic variable.
  • Then we specify format of the pagination links using setTemplate and setSelectedTemplate methods in Doctrine_Pager_Layout class.
  • Next 2 lines will get the pager object and executes the dql query using the pager object to create links.
  • Then we can traverse through the query result and print what ever fields we want in our list.
  • Last the display method will output the generated pagination links to the browser.
  • All you need to do is now include the function in your controller class and change the dql query and url according to you application.

 

This example generates basic pagination links to pages. If you want to include next,previous,first,last links and custom styles you can check out the pagination documentation in Here

Leave a Reply

Follow Us On Facebook