Reputation: 1460
I am using bonfire for my project.
I have to fetch userid
from the database according to username
and use that userid in every view.
Right now what I am doing is I have a model function and that function is called in every controller function to fetch that userid
and then userid
is set to view page.
I have to repeat that piece of code for every controller function,
My query is that Is there a way to just a userid
once in the controller and use that userid
for every view.
my controller function :-
class asd extends Admin_Controller {
protected $role;
public function _construct(){
parent::_construct();
$this->load->library('users/auth');
$this->load->model('helpdesk_model');
$role = $this->helpdesk_model->getRole($this->auth->username());
}
}
I am using Template::set('role',$role);
in my controller function
my view :-
<?php echo Template::get('role); ?>
Its showing undefined variable role :(
My model :-
function getRole($username) {
$this->db->select('role_id');
$this->db->where('username',$username);
return $this->db->get('tbl_users')->row();
}
Upvotes: 0
Views: 377
Reputation: 14428
Add a variable to the controller and initialize it in the constructor:
class Some_controller extends CI_Controller {
protected $data;
public function __construct() {
parent::__construct();
$this->load->model('some_model');
$this->data['user_id'] = $this->some_model->get_user_id();
}
public function index() {
$this->load->view('my_view', $this->data);
}
}
In the view you can access it like this:
echo $user_id;
Upvotes: 1