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

Modifying the original view file - 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

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.



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