Top 50 Codeigniter Interview Questions




What is the MVC pattern of Codeigniter?

Codeigniter follow the MVC architecture.Using MVC you can separate logic from presentation.
Model:It Contains all the database function.Using model you can insert,update,delete the data from database.
View: Generally, view ia a web page.But in codeigniter, a view can also be a page fragment like a header or footer.
It can also be an RSS page, or any other type of “page”.
Controller:serves as an intermediary between the Model, the View, and any other resources needed to process the HTTP request and generate a web page.

Describe what are the features of codeigniter?

Bellow are the important features of codeigniter:

  • Codeigniter is an open source framework and free to use.
  • It is excessively light weighted.
  • It follows the MVC architecture that allows separation between logic and presentation.
  • Codeigniter generates URLs which are clean and search engine friendly.
  • Codeigniter has full featured database classes and support for several platforms.
  • It is faster than other framework.

How to configured base url in codeigniter?

In codeigniter,the base url is configured in application/config/config.php file.
The base url can configured in the $config array with key “base_url” as given below −

$config['base_url'] = 'http://your-domain-name.com';

How to configured database in codeigniter?

In codeigniter,database can be configured in application/config/database.php file.
Codeigniter provided a multidimentional array which can setup database for different environment like development and production.
These configuration settings are stored in the array which is given below −

$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'database_name',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => TRUE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array()
);

Describe Codeigniter Architecture?

Ans:According to technical point of view, CodeIgniter is dynamically instantiation (light-weighted),
loosely coupled (components count very less on each other) and has component singularity
(each class and functions are barely focused towards their purpose).

In Codeigniter,what is the default method name?

Ans:In codeigniter,By default,always index method is being called by controller.To call a different method,write it in the controller’s
file and specify its name while calling the function.

In codeigniter,define Helper?How to load a Helper file?

Ans:A group of methods in a particular category which boost you to perform specific functions is called Helper.
In CodeIgniter,helpers are like:
1:Cookies Helpers: set and read cookies.
2:Text Helpers: perform various text formatting routines.
3:URL Helpers: helping in creating links.
Use the bellow command to load helper file:
$this->load->model(‘ModelName’);

In Codeigniter,how to access config variable?

In codeigniter,using following code to access config variable:
$this->config->item(‘variable name’);

In codeigniter how to unset session?

In codeigniter,writing the following code to unset session:
$this->session->unset_userdata(‘somename’);

In Codeigniter describe Application Flow Chart?

The following graphic illustrates how data flows throughout the system:
CodeIgniter application flow:
1:The index.php provides the front controller,which initializing the base resources needed to run CodeIgniter.
2:The Router inspect the HTTP request to include what should be done with it.
3:If a cache file exists, bypassing the normal system execution,that file is sent directly to the browser.
4:Before the application controller is loaded, the HTTP request and any user submitted data is refined for security.
5:The Controller loads the core libraries,helpers,model and any other resources which needs to process the specific request.
6:The finalized View is carried out then sent to the web browser to be seen. If caching is enabled, the view is cached first so that on following requests it can be served.

In Mysql,how to get Random Records using Codeigniter?

Ans:To get the random record in MySql,We can use this:

$this->db->order_by(‘id’,’RANDOM’);

In Codeigniter what are the Security Parameter for XSS?

  1. It has a cross-site scripting hack prevention filter.
  2. Using this filter either runs automatically or as per item basis,to filter all POST and COOKIE data that come across.
  3. The XSS filter will target the frequently used methods to trigger JavaScript or other types of code that attempt to steal cookies or other malicious activity.
  4. If it identify any doubtful thing or anything excluded is detected, it will convert the data to character entities.

In codeigniter,how many types of messages can a user log?

In codeigniter there are 3 types of messages are present:
1:Error messages->Actual errors such as user error or php errors.
2:Debug messages->Debug messages assist in debugging.
Example:You could log this as debugging,if a class has been initialized.
3:Informational messages:Lowest priority messages comes under Informational messages.In other words only giving information regarding some process.

In codeigniter,in which directory logs are saved?

In logs/directory,by default all logs are stored.To facilitated error logging the user must set the “threshold” for logging in application/config.php and logs must be writable.

In codeigniter,in which files routes are defined?

In codeigniter,all routing rules are defined in application/config/routes.php file.

In codeigniter,what are the resources that can be autoloaded.

These are the list of resources that are auto loaded automatically:
1:In libraries/directory,classes are found.
2:In helpers/directory,helper files are found.
3:In system/language/directory,languages files are found.
4:In config/directory,custom config files are found.
5:In models/folder,models are found.
In codeigniter,to autoload resoucess,open application/config/autoload.php file and add the item that the user want to load to the autoload array.

Explain ORM?What are the ORM for codeigniter?

ORM stands for object-relational-mapping.
It is a programming technique for reorganize the data between incompatible type systems using object orented programming languages.
Bellow are the ORM’s supported codeigniter framework.
1:     Data mapper
2:     Doctrine
3:     Gas ORM

In codeigniter how to delete a record?

Ans:In codeigniter,using delete function to delete a record

Example:DELETE FROM table WHERE id = $id

$conditions =['id' => $id]
$this->db->delete('table_name', $conditions);

Deleting records from more than one tables:

$id=1;
$tables = array('table1', 'table2', 'table3');
$this->db->where('id', $id);
$this->db->delete($tables);

In codeigniter,how the user can use and load codeigniter libraries?

In codeigniter,library can be loaded by using $this->load->library(‘library_name’);

Example:
Loading cart library:
$this->load->library('cart');

$data = array(
'id' => 'sku_9788B',
'qty' => 2,
'price' => 36.92,
'name' => 'T-Shirt',
'options' => array('Size' => 'XL', 'Color' => 'Blue')
);

$this->cart->insert($data);

In codeigniter what is use of is_cli()method?

Ans:In codeigniter,using is_cli()method to check request is from the command line or not.
If the application is run through the command line,it returns TRUE and it returns FALSE,if not.

In codeigniter,how to fetch the last inserted id?

In codeigniter,to fetch the last inserted id using insert_id() function.

Example:
function add_post($post_data){
$this->db->insert('posts', $post_data);
$insert_id = $this->db->insert_id();
return $insert_id;
}

In codeigniter,define Sessions and how to create and read a session?

1:Session class confesses to maintain a user’s “state” and track their activity during they are browsing your website.
2:Using session,we need to load the sessions class to controller.
3:In codeigniter,to load the session using $this->load->library(‘session’); method.
4:After loading is complete,using $this->session,session library object will be available.

Creating a session:
In codeigniter,a session can be created by using set_userdata() method of Session’s Class.set_userdata() method holds an associative array that containing
data,which is user want to add in session.

Example:
$newdata = array(
'username' => 'ruchi',
'email' => 'ruchi@some-site.com',
'logged_in' => TRUE
);
$this->session->set_userdata($newdata);

Reading a session:
In codeigniter,a session can be read the data by using $this->session->userdata(); method of session class.

Example:
$this->session->userdata('your_key');

In codeigniter,how to add and load a model?

Ans:In codeigniter,generally model will be loaded by using $this->load->model(‘model_name’); method.
After loading will be complete, user will access its methods using an object with the same name as your controller:

Example:
class Blog_controller extends CI_Controller {
public function blog()
{
$this->load->model('blog');

$data['query'] = $this->blog->get_last_ten_entries();

$this->load->view('blog', $data);
}
}

In codeigniter,how error handling works?

Ans:Using the functions,codeigniter build error reporting into the applications.
Codeigniter has an error logging class which grants error and debugging messages to be saved as text files.
These are the functions which are explain below:

1:       show_error(‘message’ [, int $statuscode= 500 ] )
Using template application/errors/errorgeneral.php file,it will display the error message given to it.
2:       show_404(‘page’ [, ‘logerror’])
Using template application/errors/error404.php file,it will display the 404 error message given to it.
3:       log_message(‘level’, ‘message’)
It writes messages to your log files. You must supply one of three “levels” in the first parameter,
indicating what type of message it is (debug, error, info), with the message itself in the second parameter.

What are the features for sending an email in codeigniter?

Ans:In codeigniter,sending email is most easier.
These are the features for sending email:

  • 1:Multiple recipients
  • 2:HTML or Plaintext email
  • 3:Word wrapping
  • 4:Attachments
  • 5:BCC Batch Mode
  • 6:Email Debugging tools
  • 7:Multiple Protocols − Mail, Sendmail, and SMTP
  • 8:TLS and SSL Encryption for SMTP
  • 9:CC and BCCs
  • 10:Priorities

How to send an email,using codeigniter?

Codeigniter is used to send an email,using $this->load->library(’email’); code to load the email library.
After loading the email library,these are the functions which are used to set basic elements to send an email.

  1. form()->It is used to set from where the email is being sent
    Example:$this->email->from(‘your@example.com’, ‘Your Name’);
  2. to()->It is used to whom the email is being sent.
    Example:$this->email->to(‘someone@example.com’);
  3. subject()->It is used to set the subject of the email.
    Example:$this->email->subject(‘Email Test’);
  4. message()->It is used to set the message of the email.
    Example:$this->email->message(‘Testing the email class.’);
  5. send()->It is used to execute to send an email.
    Example:$this->email->send();

In codeigniter,define flashdata and how to add and retrieve it?

After development of web application,only one time we need to store some data and then we remove it.
Example:To display some error message.

// Set flash data
$this->session->set_flashdata('message_name', 'This is my message');

// Get Flash data on view
$this->session->flashdata('message_name');