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

Review: the first two modules of the previous web application - 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

Before I proceed to build the three view files required to complete the PHP application created in the previous tutorial of this series, it would be useful to list the signatures of its model and controller classes, to help you recall more quickly how they were built.

That said, below I included these two classes. Have a look at them, please:

(‘user_model.php’ file – located at /application/models/ folder)


<?php

class User_model extends Model{

function User_model(){

// call the Model constructor

parent::Model();

// load database class and connect to MySQL

$this->load->database();

}

// fetch all users

function getAll(){

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

// return result set as an object array

return $query->result();

}

// fetch some users based on a predefined condition

function getWhere($field,$param){

$this->db->where($field,$param);

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

// return result set as an object array

return $query->result();

}

// get total number of users

function getNumber(){

return $this->db->count_all('users');

}

}

?>



(‘webpage.php’ file – located at /application/controllers/ folder)


<?php

class WebPage extends Controller{

function WebPage(){

// load controller parent

parent::Controller();

// load users model

$this->load->model('User_model');

// load some helpers here

}

// generate web page using partial sections

function index(){

// generate header section

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

// generate content section

$data['content']=$this->load->view('content_view',array('users'=>$this->User_model->getAll()),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);

}

}

?>


Undeniably, the tasks performed by both the model and controller classes are very easy to follow. First, the model has a few simple methods for counting and retrieving rows from a “user” MySQL table. On the other hand, the controller implements an “index()” method that uses the model’s “getAll()” method to generate the contents section of a web page.

Of course, it’s valid to point out here that each part of the web page being created is stored on a $data array, which is finally passed to a “main_page.php” view. All of these tasks, accomplished by the “WebPage” controller, naturally imply that the views need to be created accordingly.

Therefore, in the following section I’m going to build the first three views, which will be tasked with generating the header, content and footer sections of the web document.

To learn how this will be achieved, you’ll have to click on the link that appears below and read the section to come.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

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