Tag Archives: codeigniter tutorials

Codeigniter – no input file specified


Codeigniter – no input file specified : Sometimes we get error “no input file specified” in codeigniter. This problem comes due to htaccess error. You can easily fix this error by just adding the following change in .htaccess.


Codeigniter – no input file specified

Make sure “?” is added after index.php as below. After adding this it will solve your problem.

Codeigniter – no input file specified:

RewriteRule ^(.*)$ index.php?/$1 [L]

This will fix your problem.

Codeigniter Load View in Helper


Codeigniter Load View in Helper – It is very simple to load views in helper you can use ci instance to load the view file in helper function. Here in this tutorial we are going to explain how you can load the view file in helper function.


Codeigniter Load View in Helper

We have crated a custom function in helper file and we want to load views using the codeigniter ci instance.

Codeigniter Load View in Helper Example:

function renderView($viewName)
{
		$CI = &get_instance();
		$CI->load->view($viewName);		
	
}

The above function first gets the instance in $CI variable and then it uses the load view method to load the view file. The above example will work same as the loading views in codeigniter controller function.

Subquery In Codeigniter Active Records


Subquery In Codeigniter Active Records : We sometimes need to write subquery in codeigniter. There are many to write subqueries in codeigniter. The most common question we have here is how to write subqueries in Codeigniter Active Records not the core Queries. Here in this example we are going to explain how you can write subqueries in codeigniter active Recoreds.


Subquery In Codeigniter Active Records

->where() Accepts strings and passes it to the query directly. So we are going to pass subquery in this as below –

Subquery In Codeigniter Active Records

Subquery In Codeigniter Active Records Example:

$this->db->select('*')->from('Users');
$this->db->where('id NOT IN (SELECT id FROM profiles where status = "1")', NULL, FALSE);

The above example will produce the subquery and result as per subquery written.

Codeigniter libraries


Codeigniter libraries : Codeigniter libraries provides you rich functionality with ready made libraries. You can also create your own library.


Using Codeigniter libraries

$this->load->library('library_name');

For Example If We Need Form validation library

$this->load->library('form_validation');

Load Multiple Codeigniter libraries

$this->load->library(array('form_validation','email'));

You Can Also Auto-load Libraries

Go to application/config/autoload.php
Now go to the below lines and add the library name you want to automatically load.

$autoload['libraries'] = array('email','form_validation');

Codeigniter Load Helper


Codeigniter Load Helper : There are two options to load helpers in codeigniter either load inline or autoload it before using it.


Syntax : Codeigniter Load Helper

$this->load->helper('name');

Example

Load Url Helper.

$this->load->helper('url');

Codeigniter Load Multiple Helper

$this->load->helper(array('name1','name2','name3'));

Example

Load Url, form Helper.

$this->load->helper('url','form');

Auto-load Codeigniter Helper

Go to application/config/autoload.php
Go to Below Line

$autoload['helper'] = array('helper1','helper2');

Example

Load url, form Helper.

$autoload['helper'] = array('url', 'form');

How To Use

Once you have loaded the helper in the controller now you can use helper functions in view for example :


Which Will Generate a link.

Codeigniter Helpers Example


Codeigniter Helpers : Codeigniter Helpers are collection of functions which provides you help in your task.


Codeigniter Helpers are written in procedural language. Each functions in helper are independent of other.
Before Using the codeigniter functions you need to load them. You can autoload helpers which will be available
globally.

Some Codeigniter Helpers Example Are As

Url Helper, Cookie helper, Form Helper etc.


$this->load->helper('url');

Codeigniter Connect Database


Codeigniter Connect Database : You can connect to database in codeigniter as below.


Steps : Codeigniter Connect Database

Go to the application/config/databse.php

Step 1

Go to the below lines:


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

Add the above required parameter –

Hostname, Database Username and Password, Database name and other options if you need.

Step 2

Go to application/config/autoload.php
Go on the line :

$autoload['libraries'] = array();

Now Add

$autoload['libraries'] = array('database');

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 get post data


Codeigniter get post data : $this->input->post(‘field_name’); is used for getting data from post method.


Syntax : Codeigniter get post data


$this->input->post('field_name');

Suppose we have a simple form :

'post')); ?>







Suppose our controller is :

input->post('name');

          
       }
}

which will print the name posted from the form using post method