2016-04-22 181 views
0

因此,我目前有图表具有来自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> 

DB结构 http://prntscr.com/avdg9r

页面 http://prntscr.com/avdgeq

回答

1

这总是设置$ i等于0。

while($row = $result->fetch_assoc()) { 

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

} 

在while循环之外移动$ i = 0。

$i=0; 
while($row = $result->fetch_assoc()) { 

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

} 

此外,循环你的$天数组。

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++; 
} 
?> 
] 
+0

唉唉,是的,只是编辑的感谢。仍然显示空白 –

+0

非常感谢!我已经把它展示出来了,有什么想法我可以如何在1天内一起添加所有时间? –

+0

我在循环中添加了一个变量'$ total'。使用'echo $ total'来显示循环外**的总数**。 – fislerdata

0

移动你的I $循环块外,否则$ I为0每次迭代

$i=0; 
while($row = $result->fetch_assoc()) { 

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

}