15 Advance Code Igniter Interview Questions and Answer
How routing is beneficial in Code Igniter?
Routing provides a way to alter your URL and redefine it according to you. It helps in security and SEO. We can create URL having keywords and phrases influential for SEO ranking.What is a helper?
Helpers are the file having functions for specific purposes. We can directly call them in our script.There is numerous type of helper:
- Array Helper: It has functions that we use to manipulate arrays.
- File Helper: Use of this helper is to play with files as upload an image.
- HTML Helper : Html helper is used to compose HTML dom using php code.
- URL Helper: We use it to create and construct or redirect URLs.
It loads when the application start loading contains and default parts of application call. It has information to load the segments that we need to load automatically. For example, we may need to load some models automatically.
How can we connect code igniter to our database?
There is a database.php file in the config directory. We can put here our database information like database name, hostname, username and password and It will be connected.
Can we call multiple model function in a single controller function?
Yes, of course. We can call any number of model and their functions in a single controller class?
Can we write database queries in a controller?
Yes, we can write database queries directly in controllers but this is not a good practice. We should write database queries always in the model.
How can we pass data to view?
We create an array having a particular key and pass this array to load view call. We will have this data on the view page as having the name of the key given to data array.
<?php $data['employee']=array("name"=>"Dhananjay"); $this->load->view("employee_info",$data); ?>
Now on the employee_info.php page, we will have a $employee variable and that will store the data passed in the array $data in the controller.
How can we pass a parameter to controller function?
A parameter can be passed to URL segment after backslash next to function route. Suppose “admin” is a controller , “all_user” is a function and “active” is a parameter. Now we have to pass this to function that it can list the active user so we can call as given below:
<?php echo site_url("admin/all_user/active")?>
What is a library in Code Igniter?
Code Igniter library is a utility to extend the functionality and to use some predefined functions and classes. We have different libraries for different purposes as upload, database etc.
How can we execute a raw query in Code Igniter?
Using query builder library of code igniter we can execute raw query. For example,
db->query($sql); $result=$query->result_array(); ?>
What is URL helper?
We use URL helper to do manipulation with URL. We can easily create custom URL, we can extract parameters from URL, we can easily create custom anchors, form action, and page redirection.
How the database library provide strength to Code Igniter framework?
Database library has query builder functions. It works with single data and also in batch data. It accepts the array and single values as parameters. Functions are very similar to MySQL query phrases and keywords. We can use almost the query using query builder. It fetches the data in all possible formats. We can select, update, join, delete the records in very simple and robust way.
How we will submit data using a form on code igniter view?
We can give the controller function route as the form action. When we will submit this for, the controller function will be called and data will be collected using $_GET or $_POST arrays, as the method defined in the form.
How we create a session variable and how will we get that value stored in a session variable?
Code igniter has session library.It uses four types of the driver to store session data:
The file is the default storage.
After loading session library we can use session class and its method to set or get the session value.
<?php $this->load->library('session'); //set session data $this->session->set_userdata('user_session_name', 'session_value'); //check session $this->session->has_userdata('user_session_name', 'session_value'); //get session data $this->session->get_userdata('user_session_name', 'session_value'); //destroy session data $this->session->unset_userdata('user_session_name'); ?>
How can we upload any file in Code Igniter application?
Code igniter has upload library. We will use this library and set preferences in $config variable.
After initializing library we use call do_upload() function and it will complete the task.