Home arrow PHP arrow Page 3 - Moving Presentation Logic Out of Views with Code Igniter

Moving presentation logic out of views - PHP

Manipulating views with CodeIgniter is a straightforward process. In a typical situation, there’s a model that fetches some rows from one or more database tables, and a view file that receives this data through a controller class, which is finally displayed on screen, generally in the form of an HTML page. However, CodeIgniter gives PHP programmers enough freedom to handle views in several useful ways, which can speed up the development of web applications. Therefore, if you’re taking your first steps with CI and wish to learn some handy approaches that will help you work with views in a truly painless fashion, then start reading this tutorial now!

TABLE OF CONTENTS:
  1. Moving Presentation Logic Out of Views with Code Igniter
  2. Review: returning values with a previous method
  3. Moving presentation logic out of views
  4. The modified source code of the sample PHP program
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
April 16, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Since in this case I’m planning to move the “foreach” loop included within the “content_view.php” file out of it, the simplest way to do that is by implementing this part of the application straight into the controller. To demonstrate how to achieve this, below I listed the modified signature of the “WebPage” controller class, which now looks like this:


<?php

class WebPage extends Controller{

function WebPage(){

// load controller parent

parent::Controller();

// load libraries here

$this->load->database();

// load helpers here

}

// generate web page using partial sections

function index(){

// generate headers section

$data['header']=$this->load->view('header_view',array('header'=>'Header Section'),TRUE);

$query=$this->db->get('users');

if($query->num_rows > 0){

// generate content section

$data['content']=NULL;

foreach($query->result() as $user){

$data['content'].=$this->load->view('users_view',array('user'=>$user),TRUE);

}

}

// generate footer section

$data['footer']=$this->load->view('footer_view',array('footer'=>'Footer Section'),TRUE);

// generate full web page

$this->load->view('main_page',$data);

}

}

?>


As shown above, the logic implemented by the “WebPage” controller practically remains the same, except for a tiny detail: now the “foreach” loop included previously within the “content_view.php” file has been moved within the controller, and in consequence it’s necessary to create a new view that displays the data retrieved from the “users” MySQL table.

The signature of this new view file, called ”users_view.php,” looks as simple as this:


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


Here you have it. Now the controller is responsible for looping over the rows fetched from the MySQL table, and the result is stored on the $data[‘content’] array element. Lastly, each section of the web page is embedded into the “main_page.php” view, which finishes rendering the whole web document. Not too hard to understand, right?

Having implemented an alternative approach to building a basic MySQL-based web application, now it’s time to put all of its pieces together, since two of its files were modified. Thus, in the last section of this tutorial I’ll be listing for you the full source code of this sample PHP program, so you can copy/paste it and introduce your own improvements.

Now, jump forward and read the next few lines. We’re almost finished!



 
 
>>> 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 3 - Follow our Sitemap

Dev Shed Tutorial Topics: