JonYork
JonYork

Reputation: 1243

codeigniter array variable

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();
    }

Upvotes: 0

Views: 8431

Answers (3)

Phil Sturgeon
Phil Sturgeon

Reputation: 30766

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.

Upvotes: 1

Adi
Adi

Reputation: 556

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();
}

Upvotes: 1

Max Shawabkeh
Max Shawabkeh

Reputation: 38603

There is a missing comma after "$company".

EDIT: Based on the discussion in the comments, I've rewritten your code to (hopefully) get the value of the company:

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

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

  // Here we assume that the query succeeded.
  // You should probably double-check.
  $company = $result->result_array[0]['company'];

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

Upvotes: 1

Related Questions