php代码是:

$sql="SELECT `Variant`, COUNT(`Variant`) AS tsold,
      MONTHNAME(sold_date) AS mname
      FROM `vehicle_sold`
      GROUP BY MONTH(`sold_date`), `Variant` ";
while($row = mysqli_fetch_array($result)) {
    echo $row["Variant"],"---",$row["mname"],"---",$row["tsold"],"<br/>";
}


我得到以下要求的结果,它显示了每个变体每个月售罄的次数。

GLI A/T---January---1
GLI A/T---February---1
XLI M/T---February---1
GLI A/T---March---2
GLI M/T---March---2
Grande---March---1
XLI M/T---March---2
GLI A/T---April---2
GLI M/T---April---6
XLI M/T---April---3
GLI A/T---May---1
GLI M/T---May---4
Grande---May---1
GLI A/T---July---1


现在的问题是我想将此数据转移到javascript

最佳答案

如果希望将结果作为JS数组,则可以执行以下操作:

<?php
    $sql="...";
    /* Don't forget to execute the $sql query here */
    while($row = mysqli_fetch_array($result)) {
        $myData[] = $row["Variant"]."---".$row["mname"]."---".$row["tsold"];
    }
?>

<script>
    var myData = <?php echo json_encode($myData); ?>;
    alert( JSON.stringify(myData) );
</script>

关于javascript - 如何将php变量传递给javascript数组?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31451103/

10-11 07:48