Home arrow PHP arrow Page 4 - Building a Blogger with the Code Igniter PHP Framework

Defining a simple view file - PHP

People love to communicate, which may be why one of the most popular web applications you can develop is a blogger. In this six-part series, you'll learn how to develop a blogging application using PHP and the Code Igniter framework. In this first article, we'll create the bare bones structure, with plenty of code samples to help you understand the process.

TABLE OF CONTENTS:
  1. Building a Blogger with the Code Igniter PHP Framework
  2. Start building the blogger with two MySQL database tables
  3. Building a blog controller class to display blog entries
  4. Defining a simple view file
By: Alejandro Gervasio
Rating: starstarstarstarstar / 8
December 10, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

In the previous section, I explained how to build a controller class that was provided with the capacity to display all of the blog entries stored in the "blogs" MySQL table. Of course this operation can't be performed correctly if I don't first create the corresponding view file that actually renders this blog-related data.

Thus, taking into account this requisite, below I included the signature of this view, which looks like this:

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


Despite the short signature of the above view file, it actually performs a few useful tasks that are worth examining in detail. As you can see, this file starts displaying all the corresponding blog entries via a "foreach" loop, including their titles and texts, and it finishes its execution by creating dynamically a link that points to a comments section, in this case by using Code Igniter's "url" helper function.

Naturally, this section hasn't been created yet, and it must be implemented within the previous controller class. However, since this topic will be discussed in forthcoming tutorials of this series, you shouldn't be concerned about it for the moment.

Now that you have hopefully grasped how the previous view file works, you should save it to the Code Igniter /system/application/views/folder as "blogs_view.php."

So far, everything looks good, since at this stage I demonstrated how to build a simple blog application, which for the moment is only capable of displaying a group of blog entries stored previously in a MySQL table.

If you're like me, then you may want to test the application in question. To do this, simply type the following URL into your browser's address field:

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


If all goes well, you should get the following output on your browser:


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


That was pretty satisfactory, wasn't it? As shown above, this initial version of the blogger works fairly well, since it's capable or displaying all the blog entries stored on the aforementioned "blogs" MySQL table. And naturally, this process has been accomplished by using only a controller and a basic view file.

What else can you ask for? Well, actually there's many more features that need to be added to the blogger, but they'll be incorporated progressively in successive articles of this series. For the moment, study all of the examples coded here, so you can start familiarizing yourself with developing database-driven programs using Code Igniter.

Final thoughts

In this first chapter of the series I explained how to build a basic blog application with the Code Igniter PHP framework. As you saw before, this process was pretty straightforward; it only required the creation of a controller class and a view file. That was all.

In the next part, I'll be showing you how to use the pagination class that comes bundled with Code Igniter, in order to spawn across several pages all of the blog entries shown previously. Don't miss the upcoming article!



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