Welcome Guest, Not a member yet? Register   Sign In
Getting value from db for user permissions
#1

[eluser]codemonkey[/eluser]
Hello all, why doesn't this work? I have been at it for sometime now.

Code:
$query = $this->db
                                ->select('user_type')
                                ->get('users')
                                ->result();
                return $query;

                if ($query == 1) {

                    //redirect
                    redirect('site');
                    
                } elseif ($query == 2)

                //redirect
                redirect('site/otherview');

It just displays a blank page.
#2

[eluser]CroNiX[/eluser]
Because you "return $query", so nothing below that gets executed.

Also, it should be:
if ($query->user_type == 1)
#3

[eluser]codemonkey[/eluser]
Holy mother of god you are fast at replying. Thank you very much.

How can I verify what is being returned is either a number 1 or 2? I confirmed user_type is either int 1 or 2 for two different users in the db. I try logging with both and it is always defaulting to 1. Is redirect not correct for loading a controller?

Updated the code

Code:
$this->db->select('user_type');
$this->db->where('username', $this->input->post('user_name'));
$query = $this->db->get('users');

if ($query == 1) {
                    
redirect('site');

} elseif ($query == 2) {

redirect('site/other');

}

Thanks again
#4

[eluser]CroNiX[/eluser]
As I said above, your checks need to be like:
Code:
if ($query->user_type == 1)
#5

[eluser]codemonkey[/eluser]
In the updated code I added a where clause that checks the username against the post user_name when they log in.
#6

[eluser]CroNiX[/eluser]
Yes, I saw that after I replied and amended my answer regarding your variable check.
#7

[eluser]codemonkey[/eluser]
No worries.

I feel the code is not selecting the value from the user_type field but instead checking if the field is there.

Do I need to do something else to retrieve the value of the selected user_type?
#8

[eluser]CroNiX[/eluser]
You need to get a result(), or since you are only getting 1 record, a row().
Code:
$this->db->select('user_type');
$this->db->where('username', $this->input->post('user_name'));
$query = $this->db->get('users')->row();  //Add row()

if ($query->user_type == 1)
{                  
  redirect('site');
}
elseif ($query->user_type == 2)
{
  redirect('site/other');
}
#9

[eluser]codemonkey[/eluser]
Code:
A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: controllers/login.php

Line Number: 59


Maybe I'm going at this completely wrong. All I want to do is after the user logs in, verify there user_type and direct them to the correct controller function.

I'm running this code in the login controller just after it verifies they have an account.

Should I scrap it and try something else?
#10

[eluser]Ajaxboy[/eluser]
Code:
$this->db->select('user_type');
$this->db->where('username', $this->input->post('user_name'));
$query = $this->db->get('users');
$row = $query->row();

if(!$row) {
//oops nothing got pulled from the database
  redirect('default_site');
}
switch($row->user_type) {
default:
case 1:
  redirect('site');
break:
case 2:
  redirect('site/other');

break;
}




Theme © iAndrew 2016 - Forum software by © MyBB