Home arrow PHP arrow Page 3 - Paginating Blog Entries with Code Igniter

Adding records pagination capabilities - PHP

Welcome to the second installment of the series entitled “Building a blogger with the Code Igniter PHP framework.” Made up of four comprehensive tutorials, this series walks you through the development of a database-driven blogger by means of this useful package. It complements the corresponding theoretical concepts with numerous code samples.

TABLE OF CONTENTS:
  1. Paginating Blog Entries with Code Igniter
  2. Creating a basic blog application with Code Igniter
  3. Adding records pagination capabilities
  4. Modifying the original view file
By: Alejandro Gervasio
Rating: starstarstarstarstar / 4
December 17, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

In truth, providing the previous “Blogger” controller with the capacity for paginating blog entries is only a matter of using the pagination class that comes bundled with Code Igniter. It’s that simple, believe me.

However, it’s necessary to demonstrate how to perform this task with some functional code. Therefore, below I included the improved signature of the controller class, which this time will be capable of paginating all of the blog entries. Have a look at it, please:


class Blogger extends Controller{

function Blogger(){

// load controller parent

parent::Controller();

// load database class and connect to MySQL

$this->load->database();

// load pagination library

$this->load->library('pagination');

// load helper

$this->load->helper('url');

}

// display all blog entries

function blogs(){

$data['title']='Blog Entries Listing';

$data['result']=$this->db->get('blogs',3,$this->uri->segment(3));

// set pagination parameters

$config['base_url']='http://127.0.0.1/codeigniter/index.php/blogger/blogs/';

$config['total_rows']=$this->db->count_all('blogs');

$config['per_page']='3';

$config['full_tag_open']='<div id="paginglinks">';

$config['full_tag_close']='</div>';

$this->pagination->initialize($config);

// create pagination links

$data['links']=$this->pagination->create_links();

// load 'blogger_view' view

$this->load->view('blogs_view',$data);

}

}


As shown above, the controller class now incorporates a few interesting changes that deserve a closer analysis. First, apart from loading the database class and the “url” helper, it includes the pagination class, which not surprisingly is called “pagination.”

So far, the way that the constructor works is very understandable, isn’t it? Thus, move on and take a close look at the “blogs()” method. As you can see, it fetches chunks of rows from the “blogs” MySQL table, and also assigns some settings to the pagination class, such as the base URL and the number of rows that will be displayed per page, the opening and close tags that will be used for showing the paging links, and so forth.

Once this class have been configured correctly, the blog entries are embedded into the respective view, along with the paginated links in question, to be displayed later on the browser. Simple and sweet!

At this point, the controller class has the ability to span the blog entries across several pages. Of course, there are other parameters that you can use to configure the pagination class, but for the moment I’ll use the ones discussed before.

Now that the controller class has been enhanced, it should be saved to the Code Igniter /system/application/controllers/ folder as “blogger.php” for later use.

Did you do it? Then it’s time to see how to modify the view file, so it can display the paginated links as well.

Precisely, this will be the subject that I’m going to discuss in the next section, so go ahead and read the next segment.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: