Welcome Guest, Not a member yet? Register   Sign In
where condition role.
#1

Hi Coders,

i have user table with role, ADMIN AND USER now
i have a one page, that if the user login as user, he/she can view his own work while if the user login as admin he can view all user works?

any body how to implement it.


wrong condition....


PHP Code:
$this->db->where('users.fname',$this->session->userdata['logged_in']['fname'] AND 'users.role','admin'); 
 
Reply
#2

(This post was last modified: 01-08-2015, 02:31 AM by Jamie. Edit Reason: Added example )

PHP Code:
$array = array('fname' => $this->session->userdata['logged_in']['fname'], 'role' => 'admin');
$this->db->where($array);
// produces WHERE fname = 'Jamie' AND role = 'admin' 

or

PHP Code:
$this->db->where('fname',$this->session->userdata['logged_in']['fname']);
$this->db->where('role','admin');
// produces WHERE fname = 'Jamie' AND role = 'admin' 

Please see the documentation: CI3|CI2

Hope this is of some help.


Edit: On reflection the get_where() function may be neater to use.
PHP Code:
$query $this->db->get_where('users', array('fname' => $this->session->userdata['logged_in']['fname'], 'role' => 'admin')); 

An alternative way you could implement this is as follows:

PHP Code:
$query $this->db->select('fname,role')->get_where('users', array('fname' => $this->session->userdata['logged_in']['fname']));
if(
$query->num_rows() == 1) {
    
// user account exists
    
$user $this->db->row();
    if(
$user->role == 'admin') {
        
// user is admin
    
} else {
        
// user is not an admin
    
}
} else {
    
// not logged in

Jamie Bond
Full time International Development undergraduate.
Part time website developer.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB