Showing posts with label Codeigniter. Show all posts
Showing posts with label Codeigniter. Show all posts

Friday, October 23, 2009

Easy way to switch language

Create a library named MY_Controller.php and place in your system/application/libraries

and paste the code below


class Front_Controller extends Controller

{


var $current_culture='';


function Front_Controller()

{

parent::Controller();

if($this->session->userdata('language'))

{

$this->config->set_item('language',$this->session->userdata('language'));

$this->config->set_item('language_code',$this->session->userdata('language_code'));

}

$this->current_culture=$this->config->item('language');

$this->culture_code=$this->config->item('language_code');

}


function set_current_culture($language)

{

$this->session->set_userdata('language',strtolower($language));

}


}

}


Create your controller and extend it with Front_Controller class


For example:

class Home extends Front_Controller

{

function Home()

{

parent::Front_Controller();


}


function index()

{

$data['title']='Switch Language Example';

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

}


// Function if you are doing post from FORM

function language()

{

$this->set_language($this->input->post('currlang'));

}


//Function if you are selecting from the Link


function set_language($language_code)

{

$this->set_current_culture($language_code);

redirect(site_url('home'));

}


}


create your view.


You are done.


Easy way to switch language

Create a library named MY_Controller.php and place in your system/application/libraries

and paste the code below


class Front_Controller extends Controller

{


var $current_culture='';


function Front_Controller()

{

parent::Controller();

if($this->session->userdata('language'))

{

$this->config->set_item('language',$this->session->userdata('language'));

$this->config->set_item('language_code',$this->session->userdata('language_code'));

}

$this->current_culture=$this->config->item('language');

$this->culture_code=$this->config->item('language_code');

}


function set_current_culture($language)

{

$this->session->set_userdata('language',strtolower($language));

}


}

}


Create your controller and extend it with Front_Controller class


For example:

class Home extends Front_Controller

{

function Home()

{

parent::Front_Controller();


}


function index()

{

$data['title']='Switch Language Example';

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

}


// Function if you are doing post from FORM

function language()

{

$this->set_language($this->input->post('currlang'));

}


//Function if you are selecting from the Link


function set_language($language_code)

{

$this->set_current_culture($language_code);

redirect(site_url('home'));

}


}


create your view.


You are done.


Sunday, October 18, 2009

loading model in another model in CodeIgniter

Here I will show you how easy it is to load model inside another model in Codeigniter.

Say we have 2 models which are as follows:

class First_model extends Model
{
function First_model()
{
parent::Model();
}

function First_Function()
{
echo 'This is parent Function';
}
}

class Second_model extends Model
{
function Second_model()
{
parent::Model();

//Load First Model
$ci=& get_instance();
$ci->load->model('First_model');
}

function Second_Function()
{
//Call First model Function
$ci->First_model->First_Function();

}
}

You are Done.