banner image
Sedang Dalam Perbaikan

Models in CodeIgniter

In CodeIgniter, Models are the files which contains the logical parts such as database connectivity or algorithms. Using a controller function, the data included in a model file can be passed to the front end(view). Let's move on the explanation.

First you need to create two files named as Test_Controller.php and Test_model.php. As usual the first one is  the controller and the other in new model file.
Still my index function is same as in the previous article. I will change the other test_funtion to load the data included in the model class. 
Two files : 


When we consider the model file, there are two function; private and  public. The  difference between these functions is private functions can not be accessed outside its class. Here, myname() function can not be accessed outside or in another file. Think about the public function. It will return the blog name. It is important that the model class and the  file name  should be the same and the first letter should be an uppercase letter. Later I will explain the private function.

In test_function in model file, I loaded the model first. Use lowercase letters to include the model. Then I called to the model function and assigned it to a variable.  Finally it prints "My blog is TechPool".

Calling in URL:
      http://localhost/PHPProjects/CITEST/index.php/test_Controller/test_function

Private Functions in models

Now you have to do some changes in the model function; test_function. Change its function to myname() from blogname. Now we are going to use the private function. Give a suitable variable and call this function in your browser.

Code:

public function test_function() {
  $this->load->model('test_model');
  $me = $this->test_model->myname();
  echo "My name is ".$me;

}

Result : 


I told you that a private function can not be called outside the function. That is the error occurred. You have to change private to public to check whether it's working or not.

Result:


Now I think you have got an idea to create a model and  pass its data to to the front end. Next article will be based  on using a model, a view and a controller to handle some data.
Try this one before it!




Models in CodeIgniter Models in CodeIgniter Reviewed by DAL on August 12, 2017 Rating: 5

No comments:

Powered by Blogger.