views:

770

answers:

3

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  A: 

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();
}
Max Shawabkeh
if I follow what you are saying, I get the following error<code>A PHP Error was encounteredSeverity: 4096Message: Object of class CI_DB_mysql_result could not be converted to stringFilename: controllers/login.phpLine Number: 24</code>
JonYork
Well, that's another story. What line is 24 (that snippet is less than 24 lines)?
Max Shawabkeh
line 24 is 'company' => $companythe 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)
JonYork
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.
Max Shawabkeh
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
JonYork
That's because $company is a non-string (it's a CI_DB_mysql_result, apparently), and I'm guessing set_userdata() expects a string-to-string array/dictionary. When you use 'company', that's going through fine since it's a string.
Max Shawabkeh
what does that mean/how do I get a string?
JonYork
That would be something like $company->result()[0]->company. You can always examine the variables using print_r until you find how to get the actual result. Also your $this->db->where('username') should take another parameter, the value of the username to search for. Presumably that's $this->input->post('username'). So that'd be $this->db->where('username', $this->input->post('username')).
Max Shawabkeh
this is the output for print_rCI_DB_mysql_result Object ( [conn_id] => Resource id #28 [result_id] => Resource id #33 [result_array] => Array ( ) [result_object] => Array ( ) [current_row] => 0 [num_rows] => 1 [row_data] => ) what is it all saying?what im trying to do is get the data in the "company" field where username = username
JonYork
+1  A: 

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();
}
Ch4m3l3on
+1  A: 

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.

Phil Sturgeon