I'm working on a CodeIgniter project.. I want to pass a uri segment from a controller to my own library. I could not able to do it.. I tried it using session , but it won't work.please help me..
This is my controller,
<?php
class insertdata extends CI_Controller{
function __construct(){
parent::__construct();
}
//#############################################################################
function updateprop(){
$this->load->library('DataEntryForms_update');
//get the uri segment
$id = $this->uri->segment(3);
//setting the session data
$this->session->set_userdata('id',$id);
$params [] = $this->uri->segment(3);
$form_name = $this->input->post('function_name');
switch ($function_name){
case 1:
$this->dataentryforms_update->function1();
break;
case 2:
$this->dataentryforms_update->function2();
break;
}
}
}
This is my Library...
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class DataEntryForms_update{
protected $CI;
#######################################################
//gets the CI instance
function __construct(){
$this->CI =& get_instance();
}
#############################################################
function function1($params){
$this->params = $params;
print_r ($this->params);
$project_id = $this->CI->session->userdata('id');
echo $project_id;
die();
}
}
In Library I tried to get the session data,but I'm get the session data 'id' as 0 ... When I used uri->segemnt(2) . it will get the correct value, but uri->segment(3) did not work ...please help me
To access CodeIgniter's native resources within your library use the get_instance()
function.
You must have load the session library into your newly created library. You should have to include the session library into your own library. You could use the following code into your function where you are trying to access the session variable.
$CI =& get_instance();
$CI->load->helper('url');
$CI->load->library('session');
This post will surely help you to make use of sessions in your library.
in folder application\libraries\MyClass.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MyClass {
public function myfunction() {
$CI =& get_instance();
$CI->load->helper('url');
$CI->load->library('session');
// do something else below
}
}
?>
hope it works! :)