Generate line chart from SQL database output using Morris &lt;script&gt;
&lt;?php
unset($data0);
unset($data1);
unset($data2);

$sth = $dbh-&gt;prepare("
SELECT a, b, c
FROM   t
");
$sth-&gt;execute();
?&gt;

Morris.Line({
  element: 'morris-line-chart',
  data: [
  &lt;?php
  while ($row = $sth-&gt;fetch()) { ?&gt;
    { m: '&lt;?php echo $row['0']; ?&gt;', a: &lt;?php echo $row['1']; ?&gt;, b: &lt;?php echo $row['2']; ?&gt; },
  &lt;?php } ?&gt;
  ],
  xkey: 'm',
  xLabels: 'month',
  ykeys: ['a', 'b'],
  labels: ['2014', '2015']
});
&lt;/script&gt;


Is there a cleaner way to write it, especially the PHP echoing to create the JavaScript?
I like to keep the Javascript in one echo at the bottom, like this:

&lt;?php

unset($data0);
unset($data1);
unset($data2);

$sth = $dbh-&gt;prepare("SELECT a, b, c FROM t");
$sth-&gt;execute();

$data = '';
while ($row = $sth-&gt;fetch(FETCH_ASSOC)) { 
  extract($row);
  $data .= "{ m: '$c', a: $a, b: $b },".PHP_EOL;
} 

echo "&lt;script&gt;
Morris.Line({
  element: 'morris-line-chart',
  data: [$data],
  xkey: 'm',
  xLabels: 'month',
  ykeys: ['a', 'b'],
  labels: ['2014', '2015']
});
&lt;/script&gt;";


I had to make the assumptions that you're using PDO and that $c is your m in the morris line.