Welcome Guest, Not a member yet? Register   Sign In
Return users's id from the db
#1

[eluser]Mowgli[/eluser]
Hello guys,

I'm pretty new into php and i just started working with codeigniter.
Here's something i can't figure out:
Code:
$usr = $this->input->post('username');
$sql = "SELECT id FROM membership WHERE username=$usr";
if($sql['id']==37)$data['main_content'] = 'admins_area';
else $data['main_content'] = 'members_area';
$this->load->view('includes/template', $data);
Basically, what i am trying to do is: "if the username that just logged in has the id equal to 37 then he's an administrator and the admins_area will be loaded". However, even if the user with the id 37 logs in, the members_area view is being loaded.
What am i doing wrong ? Thanks in advance.
#2

[eluser]jmb727[/eluser]
[quote author="Mowgli" date="1306195973"]Hello guys,

I'm pretty new into php and i just started working with codeigniter.
Here's something i can't figure out:
Code:
$usr = $this->input->post('username');
$sql = "SELECT id FROM membership WHERE username=$usr";
if($sql['id']==37)$data['main_content'] = 'admins_area';
else $data['main_content'] = 'members_area';
$this->load->view('includes/template', $data);
Basically, what i am trying to do is: "if the username that just logged in has the id equal to 37 then he's an administrator and the admins_area will be loaded". However, even if the user with the id 37 logs in, the members_area view is being loaded.
What am i doing wrong ? Thanks in advance.[/quote]

You're SQL code wasn't executed in a query, you tried to access an array element on a string containing sql code.

I highly recommend using Active Record, used in the code below.
http://ellislab.com/codeigniter/user-gui...ecord.html

Try this.
Code:
$usr = $this->input->post('username');
$this->db->where('username', $usr);
$query = $this->db->get('membership', 1);
$row = $query->row();
if (!is_object($row)) {
   exit("User not found");
}
else {
   if ($row->id == 37) {
      $data['main_content'] = 'admins_area';
   }
   else {
      $data['main_content'] = 'members_area';
   }
}
#3

[eluser]Mowgli[/eluser]
Thanks a lot, buddy ! It's working now. And thanks for the Active Record tip, really helpful. Smile
#4

[eluser]jmb727[/eluser]
[quote author="Mowgli" date="1306202362"]Thanks a lot, buddy ! It's working now. And thanks for the Active Record tip, really helpful. Smile[/quote]

No probs. Smile




Theme © iAndrew 2016 - Forum software by © MyBB