【发布时间】:2015-07-21 14:40:34
【问题描述】:
Helo,我的 json 有问题,我在 for 中的 foreach 中有一个 foreach...不要问为什么
我的问题是 foreach foreach( $result2 as $rowx ) 只返回最后一条记录。查询正常,它从数据库返回 2 个字段,但只有最后一个被回显。
请帮忙。
if(!$location){
echo '{"locations":[';
$query = $db->prepare("SELECT * FROM locations");
$query->execute();
$result = $query -> fetchAll();
$num_rows = count($result);
for ($x = 1; $x < $num_rows; $x++) {
foreach( $result as $row ) {
$qer = "SELECT DISTINCT t.id_temp,f.id_fridge,l.id_location,t.id_sensor,f.denumire AS 'fridge_name',l.name AS 'location_name',l.address AS 'location_address',t.date,t.time,t.temp FROM fridges AS f,locations AS l,temperature AS t WHERE f.fk_location=l.id_location AND f.fk_id_sensor=t.id_sensor AND l.id_location='1' AND t.date='07/05/2015' ORDER BY t.id_temp DESC LIMIT 0,2";
$query2 = $db->prepare($qer);
$query2->execute();
$result2 = $query2 -> fetchAll();
echo '{"location_name":"'.$row[1].'","nr_fridges":'.$row[2].',"fridge":';
foreach( $result2 as $rowx ) {
}
echo '{
'.$rowx['fridge_name'].'
}';
echo '}';
}
echo "]}";
exit;
}
}
【问题讨论】:
-
检查json_encode函数,它会让debug变得更容易,因为这样真的很难读
-
空foreach(最内层)的目的是什么?