Home > Enterprise >  How to use if statement in query model
How to use if statement in query model

Time:02-03

Tried searching but haven't found an answer that solves my problem. I want to include an if condition in this query select:

    $this->datatables->select('id,username,password,email,is_active,is_role,created_at,updated_at');

    $this->datatables->add_column('action', anchor(site_url('ms_users/read/$1'), 'Read') . " | " . anchor(site_url('ms_users/update/$1'), 'Update') . " | " . anchor(site_url('ms_users/delete/$1'), 'Delete', 'onclick="javasciprt: return confirm(\'Are You Sure ?\')"'), 'id');
    return $this->datatables->generate();

I want to make like this for is_active and is_role:

    /* 
    if (is_active == 1) {
        echo "Active";
    } else {
        echo "Nonaktif";
    }
    */

    /* 
    if (is_role == 1) {
        echo "Admin";
    } else if (is_role == 2){
        echo "Users";
    }
    */

Please help, Thank you.

CodePudding user response:

maybe like this

$this->datatables->select('id,username,password,email,if(is_active=1,'Active','Nonaktif') as is_active,if(is_role=1,'Admin',if(is_role=2,'Users','Other users')) as is_role,created_at,updated_at');
  •  Tags:  
  • Related