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

Creating some basic view files - 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

If you already went through the preceding articles of this series, then you're already familiar with building simple views. The views that I plan to build now will be really simple to grasp as well. As you’ll surely recall from the controller defined in the previous section, these views must be capable of generating independently the header, main area and footer section of a web page. This is actually very easy to accomplish. 

Here are the three views that render the web page in question:


(‘header_view.php’ file – located at /application/views/ folder)


<div id="header">

<h1><?php echo $header;?></h1>

<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut.</p>

</div>



(‘content_view.php’ file – located at /application/views/ folder)


<div id="content">

<?php foreach($users 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;?>

</div>



(‘footer_view.php’ file – located at /application/views/ folder)


<div id="footer">

<h2><?php echo $footer;?></h2>

<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut.</p>

</div> 


As I said before, the signatures corresponding to the above views are fairly simple to understand. In this case, the “content_view.php” file is possibly the most complex to grasp, but all that it does is iterate over all the rows fetched from the pertinent “users” MySQL table and display the users’ first and last names, as well as their email addresses.

Due to the simple structure of the views coded before, I don’t want to spend more time explaining what they do, since that would be pretty pointless. However, as you may have noticed, there’s still one view that remains undefined, called “main_page.php,” which is precisely the one responsible for concatenating the other views.

Therefore, the last segment of the tutorial will be focused on creating this layout view, in this manner concluding this instructive journey on handling views with CodeIgniter. Now, if you want to see how this view will be coded, click on the link shown below and read the following section.



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

Dev Shed Tutorial Topics: