我想显示从我的表到高图的每一行数据,如何使用php向javascript foreach行显示数据?
<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>
答案 0 :(得分:0)
为了获得HighCharts data arranged as an array of numerical values,我要做的是使用php array_push和PHP foreach对三个不同PHP数组中的数据进行排序。
<?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']);
}
?>
然后使用数组产生一串&#34;逗号分隔值&#34;在HighCharts脚本中,使用PHP join,如下所示:
<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>
希望这有助于...
;)