PHP图表图表显示跳过的条目

时间:2014-08-28 18:13:06

标签: php jquery

我正在尝试创建这样的图http://jsfiddle.net/9mmrbjt7/1/

我创建了一个脚本,将数据添加到图表中。日期和值存储在db中,它可以正常工作。 php脚本计算日期的数量并将其用作值并将其分配给日期。

问题是如果有人错过了一天它会跳过我理解的价值。 那么如何将值零分配给跳过的日子?

2014-07-23
2014-07-23 = 2,

2014-07-24 -> wasn't submitted so the 0 should be added to the graph with the date.

2014-07-25
2014-07-25
2014-07-25= 3,

php脚本

$user_curr_id = $_SESSION['user_id'];
$sql = mysqli_query($con,"SELECT * FROM rosary WHERE user_ids = $user_curr_id ORDER BY datum ASC"); 

$array1 = array();
while($row = mysqli_fetch_array($sql)){ 
$array1[] = '"' . $row['datum'] . '"';
}

$tags = implode(', ', array_unique(array_map('trim',explode(',',implode(',',$array1))))); 

$sql =  mysqli_query($con,"SELECT datum, COUNT(datum) cnt
        FROM rosary 
        WHERE user_ids = $user_curr_id 
        GROUP BY datum;");

$result = array();

            while ($row = mysqli_fetch_array($sql)) {
                $result[] = $row['cnt'];             // add the content of field cnt
            }

在js脚本中

labels : ["Start",<?php echo $tags; ?>] -> list the dates from db

data : [0,<?php echo implode(',', $result); ?>]-> list values from db

1 个答案:

答案 0 :(得分:2)

PHP:

$sql =  mysqli_query($con,"SELECT datum, COUNT(datum) as cnt
        FROM rosary
        GROUP BY datum
        ORDER BY datum ASC;");

$result = array();

$start = null;
$end = null;
while ($row = mysqli_fetch_array($sql)) {
    $result['"'.$row['datum'].'"'] = $row['cnt'];
    if(is_null($start))  $start =  $row['datum'];
    $end =  $row['datum'];    
}

$res_array = array();
if(!is_null($start)){
    $i = strtotime($start);
    while($i <= strtotime($end)){
        $res_array['"'.date('Y-m-d',$i).'"'] = 0;
        $i = strtotime("+1 day",$i);
    }
}

foreach($result as $date => $val){
    $res_array[$date] = $val;
}

在js脚本中

labels : ["Start",<?php echo implode(',',array_keys($res_array)) ?>],
data : [0,<?php echo implode(',', array_values($res_array)); ?>],