Json只响应当前数据

时间:2017-10-11 15:53:11

标签: php json

这是我的查询,一切进展顺利。问题是只有json响应。当我打印json。只显示一个数据时。我想列出当前用户的所有数据。如果有任何帮助请告诉我,谢谢。
 $ user1 = $ wpdb-> get_results(         "从diary_user_form_storage中选择product,checked_by,submit_date,其中DATE(submit_date)= CURDATE();     &#34);

foreach ($user1 as $key => $value) {
    $productString = stripslashes($value->product);
    $checked = stripslashes($value->checked_by);
    $checked_by = json_decode($checked , true);
    $product = json_decode($productString, true);
    $checked_by = json_decode($checked , true);
    $product = json_decode($productString, true);
     $date=$value->submit_date;
    $date1 = date('Y-m-d',strtotime($date));
    $timing= date('H:i:s',strtotime($date));
    $jsonString = "";
}
 header('Content-type: text/json');
echo json_encode(
            array(
                "status" => "1",
                'user_id' =>  $user->ID,
                                'message' => 'fetched',
                "token" => $token,      
                'token' => $token.$device_id.$device_type,
                'serverUrl' => $serverUrl,  
     'option'=>$option,
     'product' => $product,
     'checked_by' =>$checked_by ,
     'submit_date'=>$timing 
    ));
?>

1 个答案:

答案 0 :(得分:1)

你误解了foreach loop是什么,可能会对variable scoping感到困惑。每次循环遍历查询元素时,都会执行以下行:

$productString = stripslashes($value->product);

一遍又一遍地覆盖同一个变量。所以,最后,当你echo json_encode(/*stuff*/)时,只输出循环最后一次迭代的内容。

幸运的是,你可以把回声放在循环中,每个部分都会被你的脚本输出。

确保在此之前放置标题,否则您的脚本将失败。标题(header('Content-type: text/json');)需要在实际内容之前发送。