Tag Archives: codeigniter tutorials

Codeigniter print session data variable array


Codeigniter print session data variable array : Codeigniter Session class is used to deal with session. We can use $this->session->userdata to get all session data in codeigniter. Here in this tutorial, we are going to explain how you can get session data in codiegniter and print the array.


Example : Codeigniter print session data variable array

There are following ways to access the array data in codeigniter.

print_r($this->session->userdata);

Or

$sessionData = $this->session->all_userdata();
print_r($sessionData);

Both will return associative array of the session.

Codeigniter print last query


Codeigniter print last query : Codeigniter provides database class to deal with database. You can use $this->db->last_query(); to print last executed query. This prints the last executed query by codeigniter. Here in this article we are going to create one example to print last query.


Codeigniter print last query | Syntax | Example

Syntax to print last query is given below-

Codeigniter print last query

$this->db->last_query(); 

Example – To print executed query at last In Codeigniter

$query = $this->db->get("users");
echo $this->db->last_query(); 

In the above example we have used get method to pull the data from users table. After execution it Will print the query – “Select * from users”. So on the same way you can print any complex query Like – Insert, Update, Delete, Joins, Union etc in Codeigniter.

Codeigniter Load Views within Views


Codeigniter load views within views : Sometimes we need to load views in another view, it is very simple to load view from another view using the $this->load->view(‘your_view’) method. Here in this article, we are going to explain to how you can load views within views.


Syntax : Codeigniter load views within views

Codeigniter Load Views within Views

We use the below syntax to load in controller, we can use the same syntax to load view in another view.

$this->load->view('your_view');

Example

Here is an example in which we have loaded another view in main view.





Welcome to the home page.

More Information Goes Here.......

On the same way we can load multiple views in any view.

Codeigniter controller constructor


Codeigniter Controller constructor : Are special functions which are called when a new object is created.


Syntax : Codeigniter controller constructor

load->helper('date');
            // More code goes here...
       }
      
      public function index(){
           // if you load date helper in contructor you need not load here 
           // all the helper date functions will be available here.
          
       }
}

Note parent::__construct(); is required to override the parent class’s constructor.

If you load date helper in contructor you need not load date helper again in any function date helper will be available in all functions inside this controller.

Codeigniter controller not found 404


Codeigniter controller not found 404 : There can be following possible reasons –
1 When your file name and class name does not match.
2 When Controller path is incorrect.
3 When incorrect routing.


1. When Class name and file name not matching.

Suppose you have a controller file named as “User.php” and your class is something different then it will not work.

example :
File name : “User.php”


It will not work because your class name is “Users” instead of “User”

Correct Class name :


This Will Work perfectly.

2. When controller path is incorrect.

Check your controller path where it is placed.

If you are using subfolders for example your controller is inside the “application/controllers/yourfolder/yourcontroller”
check that you are accessing correct path.

3. If Routing is done and routing is incorrect.

check your “application/config/routs.php” make sure your have written proper routing for controller and accessing the controller as defined in routs.

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

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 in controller constructor


Codeigniter load model in controller constructor : You can load model in codeigniter constructor instead of loading in each function. It is good practice to load the frequently used model in controller constructor. Here in this article we are going to explain how you can load model in controller constructor.


Syntax : Codeigniter load model in controller constructor

 function __contruct()
    {
        parent::__construct();
        $this->load->model('users');
    }

Example :

Here is complete example of loading model in constructor and calling its’s function in controller function.



load->model('users');
    }

    function seeAllUser()
    {
        $data['users'] = $this->users->getAllUsers();        
        $this->load->view('my_view', $data);
    }
}

Codeigniter load model in helper


Codeigniter load model in helper : We can load model in helper using CI instance. It is very simple to use CI instance and load model in helper.
Here in this article we are going to explain how you can load model in helper using get_instance(). Let us understand how to load model in helper with simple example.


You can use below method to load models in library or helper.

Codeigniter load model in helper Syntax

$CI =& get_instance();
$CI->load->model('your_model_name');
$result = $CI->your_model_name->function_name();

Codeigniter load model in helper Example

Suppose we are loading the “Users” model and calling the model function “get_my_profile”

$CI =& get_instance();
$CI->load->model('Users');
$result = $CI->Users->get_my_profile();

Codeigniter load model in library


Codeigniter load model in library : Sometimes we need to load model in library. It is very simple to load model in library using CI instance. Here in this article we are going to explain how you can load model in library.


Codeigniter load model in library Syntax

Here is an example to load model using CI instance in library-

$CI =& get_instance();
$CI->load->model('your_model_name');
$result = $CI->your_model_name->function_name();

Codeigniter load model in library Example

Suppose we are loading the “Users” model and calling the model function “get_my_profile”

$CI =& get_instance();
$CI->load->model('Users');
$result = $CI->Users->get_my_profile();