drq231358 2013-11-25 14:54
浏览 18

如何将从数据库中提取的数字传递给javascript数据部分?

I am trying to create a Bar Chart. I used some libraries that I found on github to do this. But now I am having trouble passing over the variables that I pulled from the DB (just some numbers) into the data section on the javascript. If anyone could give some tips or help that would be great.

 <!DOCTYPE html>
    <html lang="en">
        <head>
        <script src="Chart.js"></script>
        <meta charset="utf-8" />
        <title>index</title>
        </head>
        <body>
        <h1>Sales for last three months </h1>
        <?php
        require("files/connect.php");
         $queryJan = "SELECT sales_records FROM games WHERE sale_month='Jan'";
         $queryFeb = "SELECT sales_records FROM games WHERE sale_month='Feb'";
         $queryMar = "SELECT sales_records FROM games WHERE sale_month='Mar'";

         $Jan = mysqli_query($connection,$queryJan) or die(mysqli_error($connection));
         $Feb = mysqli_query($connection,$queryFeb) or die(mysqli_error($connection));
         $Mar = mysqli_query($connection,$queryMar) or die(mysqli_error($connection));

         //Information I want to pass to the javascript.
        $JanArray = mysqli_fetch_array($Jan);
        $FebArray = mysqli_fetch_array($Feb);
        $MarArray = mysqli_fetch_array($Mar);



        ?>

    <canvas id="canvas" height="450" width="600"></canvas>
        <canvas id="canvas" height="450" width="600"></canvas>
            <script>
            var barChartData = {
                labels : ["January","February","March"],
                datasets : [
                    {
                        fillColor : "rgba(220,220,220,0.5)",
                        strokeColor : "rgba(220,220,220,1)",
                        data : []
                        //I want to put the information in here and it will create a bar chart 

                    },
                    {
                        fillColor : "rgba(151,187,205,0.5)",
                        strokeColor : "rgba(151,187,205,1)",
                        data : []
                    },
                    {
                        fillColor : "rgba(151,187,205,0.5)",
                        strokeColor : "rgba(151,187,205,1)",
                        data : []
                    }
                ]

            }

        var myLine = new Chart(document.getElementById("canvas").getContext("2d")).Bar(barChartData);

        </script>



        </body>
    </html>
  • 写回答

1条回答 默认 最新

  • dpjs2005 2013-11-25 14:57
    关注

    Just get php to echo out the array as json...

    data : <?php echo json_encode($JanArray); ?>
    

    that'll do it.

    EDIT: You will need to get the data as such...

    <?php
        ...
        $JanArray = array();
        while($row = mysqli_fetch_array($Jan)) {
            $JanArray[] = $row[0]; // this fetches the data of the first column
        }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动