简体   繁体   中英

mysql outputing to json using php and group concat

I am trying to output my home temperatures table to json format. I have been successful outputing to json when just using one location using WHERE, but I can't seem to get the code right to output it and group it by location, as per example below.

Using a large mysql table that contains data like this

 --------------------------------------
|timeof              |temp   |location |
|--------------------------------------|
|2013-09-30 00:46:45 | 10.34 | outside |
|2013-09-30 00:43:45 | 18.34 | kitchen |
|2013-09-30 00:41:45 | 11.34 | outside |
|2013-09-30 00:42:34 | 19.34 | lounge  |
|2013-09-30 00:41:45 | 11.34 | outside |
|.....

and then using the following php code and mysql query, I believe I am using the correct query, but my JSON formating is a mess!

$fetch= mysql_query("
Select
  location,
  Group_Concat(timeof,temp)
From                              
  temperatures
Group By
  location
");

$result = array($name => array());
while ($row = mysql_fetch_assoc($fetch))
$result[$location][] = $row;

echo json_encode($result);

The above code is producing this JSON output, but it isn't the the way i need it;

{"":[{"location":"outside","Group_Concat(timeof,temp)":"2013-08-03 
04:51:5619.31,2013-07-23 14:51:5221.63,2013-08-03 09:51:5421.06,2013-07-23 
19:51:5122.00,2013-08-03 14:51:5222.69,2013-07-24 00:51:4921.31,2013-08-03 
16:03:0021.69,2013-08-06 07:51:2616.44,2013-07-14 20:45:2322.75,2013-07-26 
16:52:4118.38,2013-07-15 01:27:4622.38,2013-08-06 12:51:2416.56,2013-07-26"},
{"location":"kitchen","Group_Concat(timeof,message)":"2013-07-23 11:52:3017.31,
2013-09-29 18:50:3319.63,2013-08-25 01:07:1217.13,2013-10-22 11:14:3217.06,
2013-08-03 06:52:3114.44,2013-08-14 00:30:3417.31,2013-09-04 20:09:5921.13,2013-09-18 

This is how I really need the JSON output to appear;

[{name: location,data: [ [timeof, temp],[timeof, temp],[timeof, temp] ]}, {name: location,data: [ [timeof, temp],[timeof, temp],[timeof, temp] ]}]

Any idea's on what I need to change to get the correct output?

You should not be using mysql_ as the lib is deprecated. Also your code style could be improved .

But solution is anyways:

// Use an ALIAS (AS) for the GROUP_CONCAT:
$fetch= mysql_query("
Select
  location,
  Group_Concat(timeof,temp) AS data
From                              
  temperatures
Group By
  location
");

$result = array();
while ($row = mysql_fetch_assoc($fetch)) {
    // GROUP_CONCAT uses a comma as default seperator,
    // turn it back into an array:
    $row['data'] = explode(',', $row['data']);
    // Porper array dimensions:
    $result[] = array('name' => $row['location'], 'data' => $row['data']);
}

echo json_encode($result);

Why aggregate the data at the SQL tier then de-aggregate for writing into JSON?

If it were me I'd do something like....

 $qry="SELECT location, timeof, temp
    FROM termperatures
    ORDER BY location, timeof";
 ...
 $data=array();
 $x=0;
 $locn='';
 while ($r=mysqli_fetch_asoc($result)) {
       if ($r['location']!=$locn) {
          $x++;
          $data[$x]=array(
              'name'=>$r['location',
              'data'=>array()
              );
          $locn=$r['location'];
       }
       $data[$x]['data'][]=array($r['timeof'], $r['temp']);
 }
 print json_encode($data);

It'd be possible to bolt a FSM on top of this to write the JSON as a stream rather than handling it as a datagram to reduce memory pressure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM