Category Archives: Codeigniter
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-
$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
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 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();
Codeigniter class not found
Codeigniter class not found : This error occurs basically when your file name and class name does not match.
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.
Make Sure you Are “User” not “user” because of case sensetive.
Codeigniter set default controller function
Codeigniter set default controller function: Codegniter default controller is defined in routs.php which means your contrlloer will be accessed when someone access your website- for example www.example.com then the default function will be called. Here in this article we are going to explain this with example.
Codeigniter set default controller function Example
Go to config.php/routs.php and add default controller there.
$route['default_controller'] = 'Welcome/index/$1';
For example if i access :
http://localhost
It will automatically call the welcome controller and index function.
http://localhost/Welcome/index