Category Archives: Codeigniter Tutorial
Codeigniter Autoload Models
Codeigniter autoload models: You can auto-load models instead of loading it again and again.
Syntax : Codeigniter autoload models
1. Go to application/config/autoload.php
2. Go to the below lines
$autoload['model'] = array();
3. Now Add your model you want to auto-load.
$autoload['model'] = array('yourmodel_name');
For Multiple Models :
$autoload['model'] = array('yourmodel_name1','yourmodel_name2','yourmodel_name3');
Example : Codeigniter autoload models
$autoload['model'] = array('My_first_model');
Now you can access this model without loading it again in controller.
Example
My_first_model->getData(); $this->load->view('MyUsers_view', $data); } } ?>
Codeigniter controllers subfolders
Codeigniter controllers subfolders: You can organize your controller file in subfolders.
Codeigniter controllers subfolders Example
First create a folder name as “admin” inside controllers ie . “application/controllers/admin”.
Suppose we are developing the admin module in our project and we want to keep separate it from the front end files.
Now we will add each controllers in admin folder which will belong to admin module.
![]() Codeigniter controllers subfolders |
We have created a controller named as “login” inside the folder “application/controllers/admin/”
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Login extends CI_Controller { public function index() { $this->load->view('login_view'); } }
Now for Accessing the login view you have to access the url “http://localhost/admin/login” which will load the login page of the admin module.
Codeigniter Load Model
Codeigniter load model : $this->load->model(‘Your_model_name’); is used to load models in Codeigniter.
Codeigniter Load Model Syntax
$this->load->model('Your_model_name');
Note if you are using subfolders in models. Example your model file is within the “application/models/users”
folder then in this case you need to load the models in this way :
$this->load->model('your_sub_foler_name/your_model_name');
For Example : load model users_model which is inside the users folder.
$this->load->model('users/users_model');
![]() Codeigniter Load Model Example |
If you want to load model in library Read – http://tutorialsplane.com/codeigniter-load-model-in-library/
Codeigniter Load Model Example
1. Create Model :
place it in application/models folder
db->get('users',10); return $query->result_array(); } } ?>
2. Create Controller :
place it in application/controllers folder
Now Load model from and get data from users table.
load->model('My_first_model'); // now call model method $data['users'] = $this->My_first_model->getData(); $this->load->view('MyUsers_view', $data); } } ?>
3. Create view :
place it in application/views folder
Now We are going to print user data from users table.
0){ foreach($users as $user){ ?>Now Access the url “http://localhost/MyUsers/users”
Codeigniter Models Intro
Codeigniter Models Intro : Codeigniter Models are specifically used to interact with database. In Short Model is PHP Class which is used for database operations like insert, Update and Delete etc…
Codeigniter models are placed in “application/models” folder.Codeigniter Models Example
Id | Name | Phone | |
No User Found. |
db->get('users',10); return $query->result(); } } |
The Above example shoes a simple method to get 10 rows from the table “users”.