0

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

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

9 Comments

if I follow what you are saying, I get the following error <code>A PHP Error was encountered Severity: 4096 Message: Object of class CI_DB_mysql_result could not be converted to string Filename: controllers/login.php Line Number: 24</code>
Well, that's another story. What line is 24 (that snippet is less than 24 lines)?
line 24 is 'company' => $company the whole thing is 79 lines long. If I remove 'company' => $company, completely, there are no errors and run like it should (minus the company session data)
Looks to me like it's more likely the problem is happening on the "$this->session->set_userdata($data);" data since that may try (and fail) to force a cast. Constructing an array shouldn't try to do anything other than store the data.
when I run it with the line 'company' => 'company', it runs fine. its only when i go to make that a variable that it messes up
|
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.