dennis ramli dennis ramli - 22 days ago 6
CSS Question

Get data highchart from php table

i want to show every row data from my table to highchart, how can i show that data using php to javascript foreach row?

<script>
series: [{
name: 'Mie Instan',
data: [<?php foreach ($query as $row){echo $row['mie'];}]
}, {
name: 'Beras',
data: [<?php foreach ($query as $row){echo $row['beras'];}]
}, {
name: 'Telur',
data: [<?php foreach ($query as $row){echo $row['telur'];}]
}]
</script>

Answer

What I would do, in order to get the HighCharts data arranged as an array of numerical values, is to use php array_push and PHP foreach to sort the data in three different PHP arrays.

<?php
// Declare 3 new arrays.
$mie=[];
$beras=[];
$telur=[];

// Fill the arrays with the database values
foreach ($query as $row){
    array_push($mie, $row['mie']);
    array_push($beras, $row['beras']);
    array_push($telur, $row['telur']);
}
?>

Then use the arrays to produce a string of "coma separated values" in the HighCharts script, using PHP join, like this:

<script>
    series: [{
          name: 'Mie Instan',
          data: [<?php echo join(",", $mie); ?>]
     }, {
          name: 'Beras',
          data: [<?php echo join(",", $beras); ?>]
     }, {
          name: 'Telur',
          data: [<?php echo join(",", $telur); ?>]
     }]
</script>

Hoping this helps...
;)

Comments