Helo, i am having a problem with my json, i am having a foreach in a foreach in a for... dont ask why
My problem is that the foreach foreach( $result2 as $rowx ) only returns the last record. the query is ok is working fine it returns 2 fields from database , but only the last is echo'ed.
Any help please.
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;
}
}