-1

我使用了谷歌可视化插件来显示图表,因为它是从数据库中动态提取的,我使用了以下 foreach 脚本来显示结果:

var chartData = {
dynamic: [
['Date', 'Orders'],
<?php foreach($data['orders-by-date'] as $date => $orderCount): ?>
['<?php echo date('d/m', $date); ?>', <?php echo $orderCount; ?>],
<?php endforeach; ?>
[' ',0] /* Fix for IE8 */
]
};

如您所见,我必须为 IE8 添加一个修复程序,因为最后一个必须删除逗号,否则它会在 IE8 中中断。

有没有更好的方法可以从 foreach 语句的最后一项中分解逗号?我的方法有效,但它在图表末尾添加了一个空白值,这并不理想。

我希望这是有道理的!

我尝试了以下但似乎不起作用:

<script type="text/javascript">
var chartData = {
dynamic: [
['Date', 'Orders'],
<?php $fCnt = count($data); ?>
<?php foreach($data['orders-by-date'] as $date => $orderCount): ?>
['<?php echo date('d/m', $date); ?>', <?php echo $orderCount; ?>],
<?php ($date != $fCnt - 1 ? ',' : ''); ?>
<?php endforeach; ?>
]
};
</script>
4

2 回答 2

0
var chartData = {
dynamic: [
['Date', 'Orders'],
<?php 
$last = end($data['orders-by-date']);
foreach($data['orders-by-date'] as $date => $orderCount): ?>
['<?php echo date('d/m', $date); ?>', <?php echo $orderCount; ?>]<?php echo ($date != $last) ? ',' : ''; ?>
<?php endforeach; ?>
]
};
于 2013-08-02T10:58:18.937 回答
-1

尝试这个:

var chartData = {
dynamic: [

<?php
$data    = array();
$data[0] = ['Date', 'Orders'];

foreach($data['orders-by-date'] as $date => $orderCount)
{
$data[]="[".date('d/m', $date).",".$orderCount."]";
}

echo implode(",",$data);

?>
]
};
于 2013-08-02T11:04:11.207 回答