codeigniter array variable


Question

Here is my code so far, it all works except when I try to make 'company' = $company there's something im missing, and id love to know what

if($query) // if the user's credentials validated///
    {
        $this->db->where('username');
        $this->db->select('company');
        $company = $this->db->get('user');

        $data = array(
            'username' => $this->input->post('username'),
            'company' => $company
            'is_logged_in' => true
        );
        $this->session->set_userdata($data);
        redirect('site/members_area');
    }
    else
    {
        $this->index();
    }
1
0
1/21/2010 7:48:00 PM

Accepted Answer

Both the other answers fix one of the two errors in your code, this is an addition.

if($query) {
  $username = $this->input->post('username');

  $this->db->select('company')->where('username', $username);
  $result = $this->db->get('user')->row_array();

  $data = array(
      'username' => $username,
      'company' => $row['company'],
      'is_logged_in' => true
  );
  $this->session->set_userdata($data);
  redirect('site/members_area');
} else {
  $this->index();
}

See what I did there? You don't need to use result_array() then grab $query_result[0] as row_array() does that for you. And with a little method chaining thrown in for good measure you can clean up your syntax.

1
1/22/2010 11:06:32 AM

result_array is a function, not a variable. Try

if($query) {
  $username = $this->input->post('username');

  $this->db->where('username', $username);
  $this->db->select('company');
  $query_result = $this->db->get('user');

  // Here we assume that the query succeeded.
  // You should probably double-check.
  $result= $query_result->result_array();

  $data = array(
      'username' => $username,
      'company' => $result[0]['company'],
      'is_logged_in' => true
  );
  $this->session->set_userdata($data);
  redirect('site/members_area');
} else {
  $this->index();
}

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon