【问题标题】:Using database data to populate a graph使用数据库数据填充图表
【发布时间】:2016-04-22 09:50:02
【问题描述】:

所以我目前有来自 JQ 的静态数据的图表,我正在尝试使用 PHP 从数据库中获取数据。我已经开始逻辑但努力前进,任何想法都将不胜感激。到目前为止,图表输出空白,仅在我使用静态数据时才有效。

在输出数组中使用 0 作为测试

PHP<?php

$userID = $_SESSION["userid"];

$days = array();

$query = "SELECT timeSpent
FROM gymTime
WHERE userid = '$userID'";

$result = mysqli_query($conn, $query);
$i=0;
while($row = $result->fetch_assoc()) {


        $days[$i] = $row["timeSpent"];
        $i++;

     }
?

JQ <script>

// in php you simply need to create the two arrays and teh functionality will work  

// monthly set to minutes
var myTimeMon = ;
var myMonths = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];

// weekly data in mins
var myTimeWeek = [<?php echo $days[0]; ?>];
var myDays= ["Mon","Tue","Wed","Thur","Fri","Sat","Sun"];

// default values to be displayed when the page loads
var myLabels =  myDays;
var mydata =  myTimeWeek;

// store value of radiobutton
var currentValue = "m"

var displayData=[];


function contoMins(){
    // change the values of the array
    for(i=0; i<mydata.length; i++){
        mydata[i]=mydata[i]*60;
    }
    destroyChart();
}

// destroy the chart so that it does not load on top of itself
function destroyChart(){
   window.myBar.destroy();
   buildChart();
}

 function buildChart(){
        displayData = mydata;
        var barChartData = {
        labels : myLabels,
        //barValueSpacing : 25,
        //barStrokeWidth : 40,
        datasets : [
            {
                fillColor : "rgba(220,220,220,0.5)",
                strokeColor : "rgba(220,220,220,0.8)",
                highlightFill: "rgba(220,220,220,0.75)",
                highlightStroke: "rgba(220,220,220,1)",
                  data: displayData
            }
        ]

    }

        var ctx = document.getElementById("canvas").getContext("2d");
        window.myBar = new Chart(ctx).Bar(barChartData, {
              barValueSpacing : 10,
        });
    }

 buildChart();
 //sendData();  

    </script>

数据库结构 http://prntscr.com/avdg9r

网页 http://prntscr.com/avdgeq

【问题讨论】:

    标签: php jquery database


    【解决方案1】:

    这总是设置 $i 等于 0。

    while($row = $result->fetch_assoc()) {
    
        $i=0;
        $days[$i] = $row["timeSpent"];
        $i++;
    
     }
    

    将 $i = 0 移到 while 循环之外。

    $i=0;
    while($row = $result->fetch_assoc()) {
    
        $days[$i] = $row["timeSpent"];
        $i++;
    
     }
    

    另外,遍历你的 $days 数组。

    var myTimeWeek = [<?php echo $days[0]; ?>];
    

    将只显示 $days 中的第一个元素。你需要遍历数组。

    var myTimeWeek = [
    <?php
    $i = 0;
    $total = 0;
    foreach ($days as $day)
    {
      if ($i > 0) echo ', ';
      echo '"' . $day . '"';
      $total = $total + $day;
      $i++;
    }
    ?>
    ]
    

    【讨论】:

    • 非常感谢!我已经显示出来了,知道如何将 1 天内花费的所有时间加在一起吗?
    • 我在循环中添加了一个变量$total。使用echo $total 显示循环外部的总数。
    【解决方案2】:

    将你的 $i 移到循环块之外,否则每次迭代的 $i 将为 0

    $i=0;
    while($row = $result->fetch_assoc()) {
    
        $days[$i] = $row["timeSpent"];
        $i++;
    
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-01
      • 2018-02-02
      • 1970-01-01
      • 2017-03-18
      相关资源
      最近更新 更多