gpt4 book ai didi

javascript - 将 JS 代码放入 PHP while 循环中

转载 作者:行者123 更新时间:2023-11-30 08:49:42 27 4
gpt4 key购买 nike

我有一些用于显示图形数据的 JS 代码:

series: [{
name: 'Year 1800',
data: [107, 31, 635, 203, 2]
}, {
name: 'Year 1900',
data: [133, 156, 947, 408, 6]
}, {
name: 'Year 2008',
data: [973, 914, 4054, 732, 34]
}]

我需要获取数据以在 PHP 的 while 循环中显示。我试过这个:

<?php
$sql="SELECT *, COUNT(category) AS my_groupcount from tickets where deleted = '' and DAY(datetime) = '".$day."' and MONTH(datetime) = '".$month."' and YEAR(datetime) = '".$year."' group by category order by datetime ASC ";
$rs=mysql_query($sql,$conn);
while($result=mysql_fetch_array($rs))
{
echo "name: 'Cat ".$result["category"]."',";
echo "data: [".$result["my_groupcount"]."]";
echo "}, {";
}
?>

我需要在门票表中对类别列进行分组并显示每个类别的图表,但它不起作用 - 我认为这是因为在 while 循环中它以 }, { 结尾,但我需要它以 }]

结尾

我怎样才能解决这个问题 - 由于用户能够添加/删除类别,门票表中类别项目的数量一直在变化。

最佳答案

为什么不这样做:

<?php
$sql = "[.. SQL Statement ..]";
$rs = mysql_query($sql, $conn);
$json = array();

while($result = mysql_fetch_array($rs)) {
$json[] = array(
'name' => 'Cat '. $result['category'],

// This does assume that my_groupcount is an array with numbers
// i.e. array(1, 34, 54, 345)
// If not, you'll have to make it an array by doing:
// explode(', ', $result['my_groupcount'])
// This however does assume that the numbers are in
// the "12, 23" format
'data' => $result['my_groupcount'],
);
}

echo json_encode($json);

关于javascript - 将 JS 代码放入 PHP while 循环中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18983615/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com