Codeigniter - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

CodeIgniter Model

CodeIgniter Model

shape Description

CodeIgniter Model represents the data structures having the logic. Model holds all the functions on all the database queries like create, update, insert and delete. In short, interacts with the database.

Create CodeIgniter View

shape Description

All the Models are present in the folder application/models. Now for an example, create the file "hi.php" in the same folder and enter the below code. [php] <?php if(! defined('BASEPATH')) exit('No direct script access allowed'); class Hi_model extends CI_Model { public function getProfile($name) { return array("fullName" => "John", "age"=>35); } } ?> [/php] In the above model code, CI_Model is the base class for the Hi_model. In the next step, model have to be loaded from the controller "hi.php". [php] <?php if(! defined('BASEPATH')) exit('No direct script access allowed'); class Hi extends CI_Controller{ public function index(){ echo "This is my index function"; } public function one($name){ $this->load->model("hi_model"); $profile = $this->hi_model->getProfile("John"); $data = array("n" => $name); $data['profile'] = $profile; $this->load->view('one',$data); } } [/php] In the above code, to get the model from the model, the variable profile was created and used getProfile to load the name given. Then the variable profile is passed into the view class which will be as below. [php] Hello!! This is Model Example <h2><?php echo $profile["fullName"];?>,</h2> You are reading CodeIgniter tutorial in SPLessons!! [/php] Now use the below URI to get the output: [html]http://www.your-domain-name.com/Codeigniter/index.php/hi/one/John[/html] Output: In the above output, John is the data brought from the model.

Summary

shape Key Points

  • Model represents the data structures having the logic.
  • All the Models are present in the folder application/models.
  • Model data should be loaded into controller and have to be passed into the view.