Thursday, July 7, 2016

Codeigniter tutorial 5

   No comments     
categories: 
I have posted tutorials on, working with Controller and View files in the MVC Pattern Using Codeigniter.

Now I go through with Model.


In the application folder, we have a sub-folder model
Create a new file new_model.php in model folder.

<?php
Class New_model extends CI_Model{
public function getProfile($name){
return array ("fullname"=>"Nimishan","email"=>"nimishan.edu","age"=>25);
}
}
?>

Here the Base class is CI_Model. I created a function getProfile() to assign some values to an array and the variable 'name' is the function parameter for retrieving the output of the function.

Now we see how to get the output from the model to the Controller.

in the code\application\controllers edit the hello.php.

That is to create a variable to assign the values of the getProfile function which is in the model file , and also print the variable which is assigned as array elements.

<?php
class Hello extends CI_Controller{
public function index(){
$this->first();
}
public function first($name){
      $this->load->model('new_model');
$profile = $this->new_model->getProfile("shan");
print_r($profile);
}
}
?>

here I load the model file and assign the values, then print using print_r, which is used to print the output other types, not just strings.


the URL for retrieving the data is;
localhost/code/index.php/hello/first/shan

here the value shan is used as the parameter for the getProfile method.

0 comments:

Post a Comment