Codeigniter best tricks and hacks

Hi Friends,
Today I am sharing you some best tricks i have used in couple of projects, may be these things are common but we need these in all of our projects and we can find it on google but by different different authors, i am providing all the codeigniter tips tricks and hacks in this single article. if i missed any other trick then please let me know through comments, I will update it to my article.

Now please find the

Codeigniter best tricks and hacks

1: Remove index.php from url codeigniter

Solution : we need to update the .htaccess with following code:

RewriteEngine on
#RewriteBase /
RewriteRule ^(application|system|\.svn) index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
#RewriteRule ^(.*)$ index.php?/$1 [L]

2: Call Controller function or varriable from view

Solution : We need to pass the controller self object to its view to call the function or varriable of that controller . please follow instructions:

/*
controller code here
....
....
..my function 1 
..my function 2
{
  //returning any calculation 
}
*/
//Now in function 1 we are loading view 1
//and we want to call the function 2 after loaded the //view 1
//in controller file assign a self object to view
$this->data['items']=$data_from_database;
$this->data['controller']=$this;
$this->load->view('view1',$this->data);
-------------------------------------------------------------
//now in view1.php
you can call the function 2 directly as
$get_rates=$controller->function2();
//user the rate in running view

3: Db prefix in core queries

Solution : in database.php we need to pass swap_pre in configuration index in db config array. please see example:
In database.php there is a config array start with following:

$db['default'] = array(
	'dsn'	=> '',
	'hostname' => 'localhost',
        .....
	.....

So in this list there is a index named db_prefix
this is general usage in CI auto qyuiry builder class
but if we are writing the core queries then we need to pass any string like:

'swap_pre' => '{pre}',

now we can use the swap_pre value appending before table name in our query as following:

$result = $this->db->query("SELECT  * FROM {pre}records WHERE record_id ='34' and category NOT IN (78,65,98)  GROUP BY create_date");
 return $result ->result_array();

4: Call model and its function from library and helpers

Solution : we can load any model from helper or library by instantiating the ci global object using call by refrence as following:

//create a object in helper or library in codeigniter
$this->CI = &get_instance();

//now if we want to load any model then
$this->CI->load->model('Home_model');
//or load library using
$this->CI->load->library('Pdf_custom_library');

5: Get rendered html of your view in a Controller’s varriable

Solution : Use third parameter as true in this load view

Eg:

$mydata_to_view=array(“bla”=>”bla bla value”);
$myEmail_html=$this->load->view(“my_email_template”, $mydata_to_view,true);
//Now this statement will not print output instead of it will return the html string.