Reputation: 2425
i have a portal where a admin creates reseller. Each reseller can have his own users. So in users table there they are mapped with the resellerid. So now i want to show only those users which are of mapped to his ID So how to show only those users that are mapped to his account. My save function fetches all the users but instead i want to fetch users for that reseller which is loggedin instead of fetching all the records.
Following is my code for:
Controller :
public function index ()
{
// Fetch all users
$this->data['users'] = $this->reseller_m->get();
// Load view
$this->data['subview'] = 'reseller/user/index';
$this->load->view('reseller/_layout_main', $this->data);
}
Model :
public function login ()
{
$user = $this->get_by(array(
'email' => $this->input->post('email'),
'password' => $this->hash($this->input->post('password')),
), TRUE);
if (count($user)) {
// Log in user
$data = array(
'name' => $user->name,
'email' => $user->email,
'id' => $user->id,
'loggedin' => TRUE,
);
$this->session->set_userdata($data);
}
}
public function logout ()
{
$this->session->sess_destroy();
$this->session->unset_userdata('logged_in','id','name','email');
}
public function loggedin ()
{
return (bool) $this->session->userdata('loggedin');
}
The Base Model where save function exist.
public function get($id = NULL, $single = FALSE){
if ($id != NULL) {
$filter = $this->_primary_filter;
$id = $filter($id);
$this->db->where($this->_primary_key, $id);
$method = 'row';
}
elseif($single == TRUE) {
$method = 'row';
}
else {
$method = 'result';
}
return $this->db->get($this->_table_name)->$method();
}
public function get_by($where, $single = FALSE){
$this->db->where($where);
return $this->get(NULL, $single);
}
public function save($data, $id = NULL){
// Set timestamps
if ($this->_timestamps == TRUE) {
$now = date('Y-m-d H:i:s');
$id || $data['created'] = $now;
$data['modified'] = $now;
}
// Insert
if ($id === NULL) {
!isset($data[$this->_primary_key]) || $data[$this->_primary_key] = NULL;
$this->db->set($data);
$this->db->insert($this->_table_name);
$id = $this->db->insert_id();
}
// Update
else {
$filter = $this->_primary_filter;
$id = $filter($id);
$this->db->set($data);
$this->db->where($this->_primary_key, $id);
$this->db->update($this->_table_name);
}
return $id;
}
The View File:
<?php if(count($users)): foreach($users as $user): ?>
<tr class="info">
<td><?php echo anchor('admin/user/edit/' . $user->id, $user->sip_username); ?></td>
<td><?php echo anchor('admin/user/edit/' . $user->id, $user->sip_password); ?></td>
<td><?php echo anchor('admin/user/edit/' . $user->id, $user->key); ?></td>
<td><?php echo anchor('admin/user/edit/' . $user->id, $user->name); ?></td>
<td><?php echo anchor('admin/user/edit/' . $user->id, $user->status); ?></td>
<td><?php echo anchor('admin/user/edit/' . $user->id, $user->email); ?></td>
<td><?php echo anchor('admin/user/edit/' . $user->id, $user->phone); ?></td>
<td><?php echo anchor('admin/user/edit/' . $user->id, $user->balance); ?></td>
<td><?php echo anchor('admin/user/edit/' . $user->id, $user->created); ?></td>
<td><?php echo anchor('admin/user/edit/' . $user->id, $user->modified); ?></td>
<td><?php echo btn_edit('admin/user/edit/' . $user->id); ?></td>
<td><?php echo btn_delete('admin/user/delete/' . $user->id); ?></td>
</tr>
<?php endforeach; ?>
<?php else: ?>
<tr>
<td colspan="3">We could not find any users.</td>
</tr>
<?php endif; ?>
Upvotes: 0
Views: 106
Reputation: 2617
First of all when a user login.check usertype and include usertype within session. or
public function index ()
{
// Fetch all users
$session_data = $this->session->userdata('logged_in');
$id = $session_data['id'];
$data['users'] = $this->reseller_m->get($id)
// Load view
$data['subview'] = 'reseller/user/index';
$this->load->view('reseller/_layout_main', $data);
}
Your model reseller_m.php
code
function get($id)
{
$this->db->select('*');
$this->db->from('tablename');
$this->db->where('key',$id);
return $this->db->get()->result();
}
In your view file
<?php foreach($users as $user) { ?>
<?php echo $test->id;?>
Upvotes: 1