0

I've got about 10 rows in my db and 3 of which have status == to 'Pass'. The problem I've noticed with the code below is with the foreach loop. It's not wanting to work properly. I can't even get the script to send the jSON data back to the browser which means the script isn't processing past the foreach statement. Does anybody know how to fix this?

public function logsig() {
        header('Content-type:application/json');
        $postedUser = $this->input->post('username');
        $password = $this->input->post('password');
        $hashedPass = $this->encrypt->sha1($password);
        $query = $this->db->query("SELECT * FROM users WHERE username = '{$postedUser}' AND password = '{$hashedPass}'");
        foreach ($query->result() as $row) {
            if ($row->status == "Pass") {
                if ($query->num_rows() > 0) {
                    $this->session->set_userdata('logged', "1");
                    $this->session->set_userdata("username", "{$postedUser}");
                    echo json_encode(array('session_state' => true));
                } else {
                    echo json_encode(array('session_state' => false));
                }
            } elseif ($row->status == "Fail" || "Pending") {
                exit;
            }
        }
    }
2
  • Are you sure that your query is returning any results? Commented Feb 10, 2012 at 15:47
  • No right now it's not returning anything. Commented Feb 10, 2012 at 15:49

2 Answers 2

3

You're echoing the JSON in each iteration of the loop. This will make something like this:

{"session_state": true}{"session_state": false}

This is not valid JSON.

You need to build an array, and then echo json_encode() (just once) after the loop.

Also your elseif should be this:

elseif ($row->status == "Fail" || $row->status == "Pending")
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah I noticed that in the response header when i coded it a different way. Thanks!
1

Replace elseif ($row->status == "Fail" || "Pending") { with

elseif ($row->status == "Fail" || $row->status == "Pending") {

You are missing $row->status == in your second condition of elseif block

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.