Reputation: 2425
i have a Codeigniter system where Admin creates Reseller. So in reseller table values like name,country,phone,email,key,user_number, are stored. Whatever value is in user_number that much blank rows are created in users table. So reseller key is mapped to that user rows. if reseller asks for 10 user than 10 blank rows are created in user table mapped to his key.
Now once a reseller is created he can login and can edit his users i.e(he can fill in the user details)
Now my issues is that i want to fetch only the users mapped to currently loggedin reseller and not all the users in table. I dont know how to do this please help:
below is my Code where i fetch all the users for a reseller :
Controller:
public function index ()
{
// Fetch all users
$session_data = $this->session->userdata('logged_in');
$id = $session_data['id'];
$data['users'] = $this->user_m->get_users($id);
// Load view
$this->data['subview'] = 'reseller/user/index';
$this->load->view('reseller/_layout_main', $this->data);
}
The Model function:
public function get_users(){
$query = $this->db->get_where('users', array('key' => $id));
if($query -> num_rows() > 1)
{
return $query->result();
}
else
{
return false;
}
}
I have tried to fetch the users but got stucked with errors! please help me with code. i just need to show user mapped by key of reseller. So reseller should only see his own users and not others!
Upvotes: 0
Views: 432
Reputation: 2425
Finally i got the answer using sessions :)
$id = $this->session->userdata('id');
$this->db->where("key",$this->session->userdata('id'));
$this->data['users'] = $this->user_m->get();
Upvotes: 0
Reputation: 22532
You need to pass $id
argument in your model function
public function get_users($id){
Upvotes: 1
Reputation: 1741
You can set the a flag in db to know the reseller is logged in for example logged_in 1 and when the user will logout just make the logged_in to 0.
that way when you fetching the reseller just use the condition array('key' => $id, 'logged_in' => 1).
Hope that will help.
Upvotes: 0