duangangpin078794 2019-05-07 09:31
浏览 49

如何创建一个用于显示MariaDB数据的过滤器 - 在7天内?

I am using chart.js to display stats on data pushed to php with the queries. I would like create a php/sql filter to display data week by week starting on the last 7 days. I am using mysql(MariaDB).

In this scenario I have salespersons with unique IDs, and the records of each sale are being stored in the DB, including: sale id, sale date, the number of items sold, and the ID of the salesperson associated with the sale.
I used SQL query to set up a filter, which displays the number of items sold by each of the salespersons in the last 7 days.

This is my php code:

$spID = ''; //this will hold asalesperson ID
$spStats=''; //this will hold a total number of items sold by the specific salesperson in the range of 7 days

//get performance of the salesperson
$get_perf = "SELECT cast(sDate AS date) As date, spID, SUM(itemsSaled) AS spStats               
    FROM sales
    WHERE sDate >= DATE_ADD(NOW(), INTERVAL -7 DAY) AND sDate <=NOW()
    GROUP BY
    date, spID";

$agent_perf = mysqli_query($db_conn, $get_perf);    


while ($row = mysqli_fetch_array($agent_perf)) {
        $spID = $spID. '"'. $row['spID'].'",'; //label
        $spStats = $spStats. '"'. $row['spStats'].'",'; //dataset
    }

$spStats= trim($spStats, ",");

And this is my js script to represent the above as a pie chart

var ctx = document.getElementById("chart4").getContext('2d');
                var myChart4 = new Chart(ctx, {
                type: 'pie',
                data: {
                datasets: [{
                    label: 'date', //this is not being displayed
                    data: [<?php echo $spStats; ?>],
                    backgroundColor: pointBackgroundColors //takes colours from function
                }],
                //labels: [<?php echo $agent_id; ?>]
                labels: [<?php echo $spID; ?>]
                },
                options: {
                    scales: {scales:{yAxes: [{beginAtZero: false}], xAxes: [{autoskip: true, maxTicketsLimit: 20}]}},
                    tooltips:{mode: 'index'},
                    legend:{display: true, position: 'top', labels: {fontColor: 'black', fontSize: 16}}
                }
            });

So right now my graph displays my desired data for the last seven days. I would like to have a drop-down list, which will be associated with my PHP so I could scroll down to the desired data range (always 7 days), but I can't figure out of how to make the process of selecting 7 days range automatic instead of the need to assign the dates in a query. (And how will that change my js code?)

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 划分vlan后不通了
    • ¥15 GDI处理通道视频时总是带有白色锯齿
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大
    • ¥15 使用EMD去噪处理RML2016数据集时候的原理
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
    • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数