Paginating Blog Entries with Code Igniter

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.

Introduction

Code Igniter is a powerful PHP framework that allows you to build object-oriented web applications very quickly, by using the Model-View-Controller approach. Indeed, this software package comes equipped with a robust set of classes and helpers that will let you develop anything that you can think of, from simple web form processors to full-blow database-driven programs.

So, if you’re interested in learning how to take advantage of the impressive capabilities of Code Igniter, and develop a MySQL-based blog application, then don’t hesitate — start reading this article now!

With the preliminaries out of our way, it’s time to briefly review the topics that were discussed in the previous tutorial. As you’ll surely recall, during that article I demonstrated how to build a basic blog application with Code Igniter, which only required us to code two files, namely the corresponding controller class and a simple view file.

In this initial stage, the controller in question had a rather limited functionality, since it was only capable of displaying a bunch of blog entries that were previously stored in a MySQL table. Logically, the blog application in its current incarnation needs to incorporate some additional capacities, such as providing users with the ability to post comments on each particular entry, improve its visual presentation, and so forth.

But as you may guess, all of these characteristics will be introduced progressively in successive chapters. Nevertheless, in this second article of the series, I’ll be explaining how provide the controller class built in the preceding tutorial with the capability for paginating blog entries.

This specific process will require using the pagination class that comes included with Code Igniter, so don’t waste more time; start reading the next few lines. Fun is already guaranteed!


{mospagebreak title=Creating a basic blog application with Code Igniter}

Before I start showing you how to add record pagination capabilities to the blog application developed in the preceding article of the series, it’d be pretty useful to recall very quickly how its source files looked originally.

As I mentioned in the introduction, at its initial stage the blogger was comprised basically of two modules, a controller class and a view file. As with each application built with the MVC design pattern, the controller was responsible for implementing the application logic of the blogger, while the view was charged with handling its visual presentation.

But it’s time to get rid of boring explanations and show the signature of these two source files. Here they are:

// definition for ‘blogger.php’ file (located at /system/application/controllers/ folder)


class Blogger extends Controller{

function Blogger(){

// load controller parent

parent::Controller();

// load database class and connect to MySQL

$this->load->database();

// load helpers

$this->load->helper(‘url’);

}

// display all blog entries

function blogs(){

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

$data['result']=$this->db->get(‘blogs’);

// load ‘blogger_view’ view

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

}

}



// definition for ‘blogs_view.php’ file (located at /system/application/views/ folder)


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title><?php echo $title;?></title>

</head>

<body>

<h1><?php echo $title;?></h1>

<?php foreach($result->result_array() as $blog):?>

<h2><?php echo $blog['title'];?></h2>

<p><?php echo $blog['text'];?></p>

<p><?php echo anchor(‘blogger/comments/’.$blog['id'],’View Blog Comments &gt;&gt;’);?></p>

<?php endforeach;?>

</body>

</html>


At this point, the two source files listed above should look pretty familiar to you. As you’ll surely recall, the first one is the blog controller class, tasked with fetching a group of blog entries stored on a “blogs” MySQL table, and then with embedding them into the corresponding view file, for being displayed on the browser.

The view file only contains some simple presentation logic, which comes in useful for iterating and printing on screen the aforementioned blog entries.

Of course, if you’re like me, then it’s quite probable that at this very moment you’re wondering how to test this blog application. Well, it’s really simple, assuming that Code Igniter has been installed and configured correctly in your machine. Only type the following URL into your browser’s address field:

http://localhost/codeigniter/index.php/blogger/blogs/

If all goes fine, then you should get the following output:


This is the title of the first blog


This is the content of the first blog. This is the content of the first blog. This is the content of the first blog. This is the content of the first blog.

View Blog Comments >>



This is the title of the second blog


This is the content of the second blog. This is the content of the second blog. This is the content of the second blog. This is the content of the second blog.

View Blog Comments >>



This is the title of the third blog


This is the content of the third blog. This is the content of the third blog. This is the content of the third blog. This is the content of the third blog.

View Blog Comments >>



This is the title of the fourth blog


This is the content of the fourth blog. This is the content of the fourth blog. This is the title of the fourth blog. This is the title of the fourth blog.
View Blog Comments >>



This is the title of the fifth blog


This is the content of the fifth blog. This is the content of the fifth blog. This is the title of the fifth blog. This is the title of the fifth blog. This is the title of the fifth blog.
View Blog Comments >>



This is the title of the sixth blog

This is the content of the sixth blog. This is the content of the sixth blog. This is the content of the sixth blog. This is the title of the sixth blog.
View Blog Comments >>


This is the title of the seventh blog

This is the content of the seventh blog. This is the content of the seventh blog. This is the content of the seventh blog. This is the title of the seventh blog.
View Blog Comments >>


This is the title of the eight blog


This is the content of the eight blog. This is the content of the eight blog. This is the content of the eight blog. This is the title of the eight blog.
View Blog Comments >>


 

This is the title of the ninth blog


This is the content of the ninth blog. This is the content of the ninth blog. This is the content of the ninth blog. This is the title of the ninth blog.

View Blog Comments >>



This is the title of the tenth blog


This is the content of the tenth blog. This is the content of the tenth blog. This is the content of the tenth blog. This is the title of the tenth blog.

View Blog Comments >>


It’s really amazing to see how easy it is to build a simple blog application using Code Igniter. However, I don’t want to be stuck in this moment, so it’s time to move on and see how to modify the signature of the controller class that you learned before, to provide it with the ability to paginate all of the blog entries.

To learn how this feature will be incorporated into the program, click on the link below and read the following section.


{mospagebreak title=Adding records pagination capabilities}

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.

{mospagebreak title=Modifying the original view file}

Provided that you already grasped how the previous controller class paginates all of the blog entries via the corresponding pagination class, now it’s time to modify the signature of the view file so that it can display the pertinent paginated links too.

As you saw before, the controller passes to the view a $data array that contains an element referenced as $data[‘links’]. This one is actually the string that renders the paginated links, so in order to display them along with the blog entries, the view should be modified in the following way:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title><?php echo $title;?></title>

</head>

<body>

<h1><?php echo $title;?></h1>

<?php foreach($result->result_array() as $blog):?>

<h2><?php echo $blog['title'];?></h2>

<p><?php echo $blog['text'];?></p>

<p><?php echo anchor(‘blogger/comments/’.$blog['id'],’View Blog Comments &gt;&gt;’);?></p>

<?php endforeach;?>

<?php echo $links;?>

</body>

</html>


Now, all of the blog entries fetched from the “blogs” MySQL table will be displayed across several pages, along with the paginated links. In addition, you should first save the improved version of this view to the Code Igniter /system/application/views/ folder as “blogs_view.php.”

Once you’ve done this, type the following URL into your browser’s address field:


http://localhost/codeigniter/index.php/blogger/blogs/


If all goes as expected, you should get the below output:


 


Here you have it! At this point you should feel pretty satisfied, since now the blog application is capable of displaying a group of blog entries which have been paginated. Definitely, that was a neat improvement, right?

To wrap up this tutorial, I encourage you to tweak all of the code samples I’ve included here, so you can introduce more improvements to the blog application.

Final thoughts

In this second chapter of the series, I demonstrated how to add record pagination capabilities to the blog application built earlier. As you saw for yourself, this process only required using the Code Igniter’s pagination class, and introducing a few simple changes to the controller and the view as well.

In the upcoming article, I’ll continue adding more features to the blogger, such as the ability to post comments on each blog entry.

However, to learn how this will be done, you’ll have to read the next part! See you next week.

[gp-comments width="770" linklove="off" ]

antalya escort bayan antalya escort bayan Antalya escort diyarbakir escort