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

Creating a basic blog application with Code Igniter - 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

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.




 
 
>>> 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: