Php while循环而不是foreach来获取数组数据

时间:2014-12-02 15:28:58

标签: php arrays foreach while-loop

在后端我有休息api,代码是:

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

并且使用此代码我只从数组获得第一个数据:

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

现在当我想要获取我写的所有数据时:

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

所以与foreach循环相同的代码,但后来我得到:Unexpected token {

这有什么问题?我如何在这里实现WHILE循环而不是foreach ???

更新功能:

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

1 个答案:

答案 0 :(得分:2)

将结果存储为数组,并在循环结束时发送整个数组:

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

echoRespnse()应该json_encode $ items数组。

此外,在getAllUserTasks()函数中,您应该使用while循环从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;
}