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.

  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



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


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
escort Bursa Bursa escort Antalya eskort


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