-1

If I place the echo "h"; inside the if statement I get nothing returned. Also if I place a WHERE clause inside the SQL code and use that method, I also get nothing returned inside the foreach loop. Any ideas as to why this is happening? I only need the user that matches the current user logged in. And what the script's doing is looping through all the rows in the users table

public function dcr() {
    // pass all dashboard accesses through this function
    $username = $this->session->userdata("username");
    $query = $this->db->query("SELECT * FROM users");
    foreach ($query->result() as $row) {
                    echo "h";
        if ($username == $row->username) {
                $data = array('firstname' => $row->firstname);
                $this->load->view('dashboard', $data);
            }
    }
}
3
  • Are there users in your table? Commented Feb 7, 2012 at 1:44
  • That depends on your users table, the wording of you WHERE, what $username is, etc. Posting them would help finding an answer. Commented Feb 7, 2012 at 1:45
  • I set the session wrong in my Ci controller. I got it working now. Commented Feb 7, 2012 at 2:11

1 Answer 1

1

This should do the trick (have not tested), you don't need the if statement, just use function get_where, as the code will become cleaner and readable.

public function dcr() {
    // pass all dashboard accesses through this function
    $username = $this->session->userdata("username");
    $query = $this->db->get_where('users', array('username' => $username));
    foreach ($query->result() as $row) {
        $data = array('firstname' => $row->firstname);
        $this->load->view('dashboard', $data);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Querying the database is not the responsibility of the controller. This is the purpose of the model layer in MVC architecture.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.