Home arrow PHP arrow Page 3 - Handling Views with CodeIgniter

Creating a content view - PHP

The Code Igniter framework makes it easy for developers to implement a Model-View-Controller design pattern in PHP. This concept can be difficult for newcomers to grasp, so this seven-part series of articles will focus on one aspect of it: views, and the many clever ways you can handle them with Code Igniter. Welcome to the first part.

TABLE OF CONTENTS:
  1. Handling Views with CodeIgniter
  2. Preparing views to be rendered on screen
  3. Creating a content view
  4. Building a web page controller class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 9
March 19, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

In the previous section, I explained how to build a couple of static views that will be used later on for generating the header and footer parts of a simple web page. However, the only view file that remains undefined is the one tasked with rendering the main area of this page. In this situation, I’m going to populate this area with content from a MySQL table called “users,” which I also used in some other PHP articles published here at the prestigious Developer Shed network.

As a quick reminder, the structure of this table looked like this:



Now that you hopefully recalled how this example table was filled with some trivial records, it’s time to build the view file that loops over each of them. Here it is:


<div id="content">

<?php if($users->num_rows > 0):?>

<?php foreach($users->result() as $user):?>

<p><strong>First Name: </strong><?php echo $user->firstname;?></p>

<p><strong>Last Name: </strong><?php echo $user->lastname;?></p>

<p><strong>Email: </strong><?php echo $user->email;?></p>

<hr />

<?php endforeach;?>

<?php endif;?>

</div>


Undoubtedly, the above view should be fairly easy to grasp for you, since all that it does is iterate over each user fetched from the previous MySQL table and display their first and last names, and their email address, on screen. Before I forget, please save this view under the /application/views/ folder of CodeIgniter, so it can be used later on.

Done? Great. Having created the view file that generates the dynamic section of the sample web page, the next step I’m going to take will consist of defining a simple controller class. As you’ll see in a moment, this controller will be responsible for loading the three views in a sequential fashion, in this way generating the entire web document.

This procedure will be shown in the upcoming section of this article, so if you wish to learn more, please read the next few lines.



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

blog comments powered by Disqus
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 4 - Follow our Sitemap

Dev Shed Tutorial Topics: