Home arrow PHP arrow Page 4 - Embedding Model Data in Views with Code Igniter

Finishing the sample PHP application with CodeIgniter - PHP

Welcome to the conclusion of a seven-part series on handling views with the Code Igniter PHP framework. If you're learning how to use the Model-View-Controller schema, this series of articles can help you get a better grasp of how to use it to quickly and dynamically generate web pages. In this part, we will finish building the database-driven application we discussed in the previous part.

  1. Embedding Model Data in Views with Code Igniter
  2. Review: the first two modules of the previous web application
  3. Creating some basic view files
  4. Finishing the sample PHP application with CodeIgniter
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
April 30, 2009

print this article



In the previous section, I created the three views that will be used for generating independently several section of a web page. Therefore, the last step that I’m going to take will consist of building the layout view, which was called “main_page.php.” Obviously, this particular file is tasked with concatenating the contents of the other views, meaning that its signature will be very simple to code.

Below you'll see the entire source code corresponding to this layout file. Have a look at it, please:

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


<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Sample Web Page</title>

<style type="text/css">


padding: 0;

margin: 0;

background: #999;



width: 600px;

margin: 0 auto;



padding: 10px;

background: #eee;



padding: 10px;

background: #9cf;



padding: 10px;

background: #eee;



font: bold 2em Arial, Helvetica, sans-serif;

margin: 0 0 18px 0;

color: #039;



font: bold 1.5em Arial, Helvetica, sans-serif;

margin: 0 0 18px 0;



font: normal .8em Arial, Helvetica, sans-serif;

margin: 0 0 18px 0;





<div id="container">


echo $header.$content.$footer;





If you were expecting to see a lengthy view, you might feel a little disappointed. Apart from including a few basic CSS styles, the above layout file contains only one “echo” PHP statement that appends the contents of the other views to generate the header, main area and footer section of the web page. Of course, this isn’t the only approach that can be utilized for building front-ends dynamically with CodeIgniter, but it certainly is a common one.

Finally, now that you've learned how to handle views in a few clever ways, feel free to tweak all of the code samples developed in this tutorial. Doing this should give you a more solid understanding of how to create web pages with CI. Whatever you do from this point onward, one thing is for sure: fun is already guaranteed!

Final thoughts

It’s really hard to believe, but we’ve come to the end of this series. If you’ve been a patient reader and examined in depth all of the code samples included in these tutorials, then by this time should have a clear idea of how to process views with CodeIgniter. As you can see, there is not just one right way to handle views; CI offers several approaches that can be used to suit the requirements of a specific PHP application.

From my point of view as a web developer, I find returning strings from views a very useful method for building web pages where static and dynamic contents are generated independently, but again, this is only a matter of personal preference.

See you in the next PHP development tutorial!

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