2014-12-01 107 views
0

迴應我寫的:獲得來自陣列

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

      // fetching all user tasks 
      $result = $db->getAllUserTasks($user_id); 

      $response["error"] = false; 
      $response["tasks"] = array(); 

      // looping through result and preparing tasks array 
      $response["error"] = false; 
       $response["id"] = $result["id"]; 
       $response["task"] = $result["task"]; 
       $response["status"] = $result["status"]; 
       $response["createdAt"] = $result["created_at"]; 
       echoRespnse(200, $response); 
     }); 

但是這個代碼,我只得到第一個結果[0] ...

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

如何讓同時在結果循環獲取所有數據從結果?

+0

您使用的是什麼框架? – 2014-12-01 17:49:58

+0

苗條的框架... – LaraBeginer 2014-12-01 17:50:58

+0

你沒有循環任何東西。你應該傳入$ user_id而不是使用全局。 – 2014-12-01 17:51:50

回答

1
foreach($result as $row){ 
echo $row["id"]; 
//-- and so on 
} 
+0

我現在得到的只是:{ 錯誤:錯誤 任務:[0] } – LaraBeginer 2014-12-01 17:53:52