Welcome Guest, Not a member yet? Register   Sign In
Selecting?
#1

[eluser]Andeh17[/eluser]
How would you select from the database if a users is_admin status is equal to 1?

I currently have these...

controller:
Code:
function is_admin()
{
  $session = $this->session->userdata('username');
  $query = $this->db->query("SELECT * FROM `users` WHERE `username` = '{$session}'");

  foreach($query->result() as $row)
  {
      if($row->is_admin == 1)
      {
       return true;
      }
      else
      {
       return false;
      }
  }
}

view:
Code:
if($this->is_admin())
  {
   echo 'you are an administrator';
  }

Thanks in advance.
#2

[eluser]meigwilym[/eluser]
I think that's ok, but a more concise approach might be

Code:
function is_admin()
{
  $session = $this->session->userdata('username');
  return $this->db->query("SELECT * FROM `users` WHERE `username` = '{$session}' AND is_admin = '1'")->row();

  }

Anyone else any thoughts? (in case I'm wrong)

Cheers,

Mei
#3

[eluser]Andeh17[/eluser]
[quote author="meigwilym" date="1328195989"]I think that's ok, but a more concise approach might be

Code:
function is_admin()
{
  $session = $this->session->userdata('username');
  return $this->db->query("SELECT * FROM `users` WHERE `username` = '{$session}' AND is_admin = '1'")->row();

  }

Anyone else any thoughts? (in case I'm wrong)

Cheers,

Mei[/quote]

Sorry I forgot to include the error, thanks for that but it still isn't working.

Fatal error: Call to undefined method CI_Loader::is_admin()

And it is throwing it on this line...

if($this->is_admin())

Am I supposed to load the controller or something to call the function? I'm so confused right now.
#4

[eluser]PhilTem[/eluser]
I prefer

Code:
function is_admin($username)
{
    return $this->db
                    ->from('users')
                    ->where('username', $username)
                    ->where('is_admin', '1')
                    ->count_all_results() == 1;

}

because it's much shorter (if you compress code) and you actually don't run a real select query but more a 'SELECT COUNT(*) FROM' query. Which is a little fail safer for sql-injections since it only returns a number and no full rows.

But both queries are right. I like mine just a little more since it only counts matches and doesn't select them Wink

Refer to CI user's guide -> database class -> active record.
#5

[eluser]Andeh17[/eluser]
[quote author="PhilTem" date="1328196906"]I prefer

Code:
function is_admin($username)
{
    return $this->db
                    ->from('users')
                    ->where('username', $username)
                    ->where('is_admin', '1')
                    ->count_all_results() == 1;

}

because it's much shorter (if you compress code) and you actually don't run a real select query but more a 'SELECT COUNT(*) FROM' query. Which is a little fail safer for sql-injections since it only returns a number and no full rows.

But both queries are right. I like mine just a little more since it only counts matches and doesn't select them Wink

Refer to CI user's guide -> database class -> active record.[/quote]

But that still doesn't fix why i'm receiving..

Fatal error: Call to undefined method CI_Loader::is_admin() in /Applications/XAMPP/xamppfiles/htdocs/codeigniter/application/views/logged_in_area.php on line 15

What is on that line:
if($this->is_admin($this->session->userdata('username')))

Cheers anyway.
#6

[eluser]InsiteFX[/eluser]
And what does - Call to undefined method mean?

This is basic PHP!
#7

[eluser]Bhashkar Yadav[/eluser]
this error is related to code into view file :
Code:
if($this->is_admin())
{
   echo 'you are an administrator';
}
#8

[eluser]Andeh17[/eluser]
[quote author="InsiteFX" date="1328198090"]And what does - Call to undefined method mean?

This is basic PHP!
[/quote]

Yes it's basic PHP, i'm new to CodeIgniter and frameworks so cut me some slack.

And I just can't see where the errors coming from, not my fault.
#9

[eluser]CroNiX[/eluser]
What is the column name in your table that tells if they are admin or not?
#10

[eluser]Andeh17[/eluser]
[quote author="CroNiX" date="1328203609"]What is the column name in your table that tells if they are admin or not?[/quote]

@CroNiX it's is_admin




Theme © iAndrew 2016 - Forum software by © MyBB