CodeIgniter Forums
forum, admin rank, need help - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: forum, admin rank, need help (/showthread.php?tid=19700)



forum, admin rank, need help - El Forum - 06-16-2009

[eluser]benfike[/eluser]
Hi! I have an forum, php beast from this page. And i wanted change admin ranks...
My site have 3 rank.
rank 0= user
rank 1= only news writer (admin)
rank 2= full admin rank

and here the forum , user class file:

function is_admin($id = '')
{
$this->CI->db->where('id', $id);
$this->CI->db->select('rank');
$query = $this->CI->db->get('users');
if($query->num_rows > 0)
{
$rank = $query->row();
return $rank->rank;
}

return false;
}

and i wanted to change this, where rank=0 not admin and where rank 1 or 2 Admin.

Help me pls


forum, admin rank, need help - El Forum - 06-16-2009

[eluser]Dam1an[/eluser]
I would use something like this
Code:
function is_admin($id = -1) {
    // If no ID is specified, they can't be an admin
    if($id == -1) return false;
    
    // Get the row for the user
    $this->CI->db->where('id', $id);
    $this->CI->db->select('rank');
    $query = $this->CI->db->get('users');
    if($query->num_rows > 0) {
        $user = $query->row();
        
        // They're admin if the rank is greater then 0
        return $user->rank > 0;
    } else {
        // That user ID didn't exist
        return false;
    }
}



forum, admin rank, need help - El Forum - 06-16-2009

[eluser]Phil Sturgeon[/eluser]
This way is a lot cleaner.

Code:
function is_admin($id = NULL) {
    // If no ID is specified, they can't be an admin
    if($id === NULL) return FALSE;
    
    // Get the row for the user
    $this->CI->db->where('id', $id);
    $this->CI->db->where('rank >', 0);
    
    return $this->CI->db->count_all_results('users') > 0;
}



forum, admin rank, need help - El Forum - 06-16-2009

[eluser]Dam1an[/eluser]
Ah yes, the good ol' count methods, why did that not occur to me?
I could use that in a fair few places to clean up my own code lol


forum, admin rank, need help - El Forum - 06-16-2009

[eluser]benfike[/eluser]
Thank you


forum, admin rank, need help - El Forum - 06-16-2009

[eluser]benfike[/eluser]
I have an another Question...

I have an avatar field in my users table. And on forums, when the user not upload any avatar its bugged, and don't show a default avatar. How can i use default avatar when avatar == ''?