Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

On backend I have rest api, the code is:

$app->get('/tasks', 'authenticate', function() {
            global $user_id;
            $response = array();
            $db = new DbHandler();

            // fetch task
            $result = $db->getAllUserTasks($user_id);

            $result = $db->getAllUserTasks($user_id);

        if ($result != NULL) {

            $response["error"] = false;
            $response["id"] = $result["id"];
            $response["task"] = $result["task"];
            $response["status"] = $result["status"];
            $response["createdAt"] = $result["created_at"];
            echoRespnse(200, $response);
        } else {
            $response["error"] = true;
            $response["message"] = "The requested resource doesn't exists";
            echoRespnse(404, $response);
        }
        });

and with this code I get just first data from array:

{
error: false
id: 2
task: "Create something"
status: 0
createdAt: "2014-12-01 01:58:42"
}

now when I want to fetch all data I write:

if ($result != NULL) {
                foreach ($result as $rez) {
                $response["error"] = false;
                $response["id"] = $rez["id"];
                $response["task"] = $rez["task"];
                $response["status"] = $rez["status"];
                $response["createdAt"] = $rez["created_at"];
                echoRespnse(200, $response);
                }
            } else {
                $response["error"] = true;
                $response["message"] = "The requested resource doesn't exists";
                echoRespnse(404, $response);
            }

so the same code with foreach loop but then I get: Unexpected token {

What is wrong with that? How I can implement here WHILE loop instead foreach ???

UPDATE with function:

public function getAllUserTasks($user_id) {
        $stmt = $this->conn->prepare("SELECT t.* FROM tasks t, user_tasks ut WHERE t.id = ut.task_id AND ut.user_id = ?");
        $stmt->bind_param("i", $user_id);
        if ($stmt->execute()) {
            $res = array();
            $stmt->bind_result($id, $task, $status, $created_at);
            // TODO
            // $task = $stmt->get_result()->fetch_assoc();
            $stmt->fetch();
            $res["id"] = $id;
            $res["task"] = $task;
            $res["status"] = $status;
            $res["created_at"] = $created_at;
            $item[] = $res;
            $stmt->close();
            return $item;
        } else {
            return NULL;
        }
    }
share|improve this question
    
only run '$result = $db->getAllUserTasks(...' once. If you 'var_dump($result)' you will see what the structure of the returned data actually is. This will help to work out why the 'foreach loop' is confused. –  Ryan Vincent Dec 2 at 15:38
    
Where exactly do you get "Unexpected token {"? –  Eric Ping Dec 2 at 15:40
    
yes, I do this sorry for mistake in cde –  LaraBeginer Dec 2 at 15:41

1 Answer 1

up vote 1 down vote accepted

Store the results as an array and send the entire array when the loop ends:

if ($result != NULL) {
    $items = array();

    foreach ($result as $rez) {
        $response["error"] = false;
        $response["id"] = $rez["id"];
        $response["task"] = $rez["task"];
        $response["status"] = $rez["status"];
        $response["createdAt"] = $rez["created_at"];

        $items[] = $response;
    }

    echoRespnse(200, $items);
} else {
    $response["error"] = true;
    $response["message"] = "The requested resource doesn't exists";
    echoRespnse(404, $response);
}

And echoRespnse() should json_encode the $items array.

Also, in your getAllUserTasks() function, you should use a while loop to fetch all the results from db:

if ($stmt->execute()) {
    $items = $res = array();

    $stmt->bind_result($id, $task, $status, $created_at);

    // fetch values
    while ($stmt->fetch()) {
        $res["id"] = $id;
        $res["task"] = $task;
        $res["status"] = $status;
        $res["created_at"] = $created_at;

        $items[] = $res;
    }

    $stmt->close();

    return $items;
} else {
    return NULL;
}
share|improve this answer
    
ok, but now I get wrong data like this: [4] 0: { error: false id: null task: null status: null createdAt: null }- 1: { error: false id: "C" task: "C" status: "C" createdAt: "C" }- 2: { error: false id: null task: null status: null createdAt: null }- 3: { error: false id: "2" task: "2" status: "2" createdAt: "2" } –  LaraBeginer Dec 2 at 15:50
1  
@LaraBeginer Maybe you implemented my recommendation in the wrong place ? See my updated example. –  Alexandru Guzinschi Dec 2 at 15:53
    
again I get just: [1] 0: { error: false id: 2 task: "Create something" status: 0 createdAt: "2014-12-01 01:58:42" } so just first data (first user task from tasks) I will update code with funcction getAllUserTasks () now –  LaraBeginer Dec 2 at 15:57
    
I update code with function getAllUserTasks () ... –  LaraBeginer Dec 2 at 15:58
1  
@LaraBeginer See my updated answer. –  Alexandru Guzinschi Dec 2 at 16:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.