dtcuv8044 2018-08-17 22:09
浏览 86

使用AJAX请求SQL数据

I am using chart.js to create graphs. I'm trying to generate a graph for bdi vs date for specific customers based on their unique IDs. When you visit customer.php that specific customers ID can be found in the URL. I'm trying to request the bdi vs. date data from data.php and query it to generate a line graph.

This is what I have so far:

Customer.php (Chart.js Code);

 $(document).ready(function(){
    $.ajax({
            
            url: "charts/data.php",
            method: "GET",
            success: function(data) {
            console.log(data);
            var bdi = [];
            var date = [];

            for(var i in data) {
                date.push( data[i].date);
                bdi.push(data[i].bdi);
            }

            var chartdata = {
                labels: date,
                datasets : [
                    {
                        label: 'BDI',
                        backgroundColor: 'rgba(239, 243, 255, 0.75)',
                        borderColor: 'rgba(84, 132, 255, 0.75)',
                        hoverBackgroundColor: 'rgba(200, 200, 200, 1)',
                        hoverBorderColor: 'rgba(200, 200, 200, 1)',
                        data: bdi
                    }
                ]
            };

            var ctx = $("#mycanvas");

            var barGraph = new Chart(ctx, {
                type: 'line',
                data: chartdata,
                options: {
                    responsive: true,
        legend: {
            position: 'bottom',
            
        },
        scales: {
            yAxes: [{
                ticks: {
                    fontColor: "rgba(0,0,0,0.5)",
                    fontStyle: "bold",
                    beginAtZero: true,
                    maxTicksLimit: 5,
                    padding: 20
                },
                gridLines: {
                    drawTicks: false,
                    drawBorder: false,
                    
                }
}],
            xAxes: [{
                gridLines: {
                    zeroLineColor: "transparent",
                    display: false
                        },
                ticks: {
                    padding: 20,
                    fontColor: "rgba(0,0,0,0.5)",
                    fontStyle: "bold"
                }
            }]
        },
                    
            tooltips: {
                backgroundColor: 'rgba(255,255,255)',
                titleFontColor: 'rgb(184,189,201)',
                bodyFontColor: 'black',
                displayColors: false,
                borderColor: 'rgb(214,217,225)',
                borderWidth: 1,
                caretSize: 5,
                cornerRadius: 2,
                xPadding: 10,
                yPadding: 10
            }
        }
});
        },
        error: function(data) {
            console.log(data);
        }
    });
});
            

data.php:

<?php
session_start();
//setting header to json
header('Content-Type: application/json');
require_once '../config/config.php';
require_once '../includes/auth_validate.php';


$cid = htmlentities ($_GET['customer_id']);
//get connection

//query to get data from the table
                $sql = sprintf("SELECT treatment_log.bdi, treatment_log.date FROM treatment_log WHERE treatment_fk = ? ORDER BY created_at");

                $stmt = mysqli_stmt_init($conn);
                mysqli_stmt_prepare($stmt, $sql);
                mysqli_stmt_bind_param($stmt, "i", $cid);
                mysqli_stmt_execute($stmt);
                $data = array();
                mysqli_stmt_bind_result($stmt, $bdi, $date);
                while(mysqli_stmt_fetch($stmt)) {
                  $data[] = array('bdi' => $bdi, 'date' => $date);
                }
                //free memory associated with result
                $result->close();

                //now print the data
                print json_encode($data); 

I cant seem to generate a graph. I'm not sure what I'm doing wrong!

Example of data to be graphed:

[{"bdi":"4","date":"2018-07-11"},{"bdi":"1","date":"2018-07-21"},{"bdi":"5","date":"2018-07-21"},{"bdi":"34","date":"2018-07-21"},{"bdi":"34","date":"2018-07-21"},{"bdi":"3","date":"2018-07-22"},{"bdi":"2","date":"2018-07-23"},{"bdi":"12","date":"2018-07-23"},{"bdi":"3","date":"2018-07-24"},{"bdi":"2","date":"2018-07-25"},{"bdi":"12","date":"2018-07-30"},{"bdi":"3","date":"2018-07-30"},{"bdi":"4","date":"2018-07-30"},{"bdi":"11","date":"2018-07-30"}]  

</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 R语言Rstudio突然无法启动
    • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
    • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
    • ¥15 用windows做服务的同志有吗
    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值