PHP中的json响应中缺少数据

时间:2016-11-29 09:29:59

标签: php json

我正在做的是,在日历上列出表格中的事件。有些活动结束时间超过24小时,因此我将日期和列表拆分为日历上的单独事件。目前我的问题是标题没有显示在json响应中(请检查响应"title":"test" "title":"")。

我需要的是基于ID 1:

"title":"test" "title":"test"

例如id 1:

表ID = 1:它的startTime是14804070619 (2016-11-29 08:11:01),endTime是1480500661 (2016-11-30 10:11:01)

分割ID 1后

"start":"2016-11-29T09:11:01" and "end":"2016-11-29T23:59:59" "start":"2016-11-30T00:00:00" and "end":"2016-11-30T10:11:01"

ID 1的响应

[{"title":"test","start":"2016-11-29T09:11:01","end":"2016-11-29T23:59:59","class":"bg-complete-lighter "},{"title":"","start":"2016-11-30T00:00:00","end":"2016-11-30T10:11:01","class":"bg-complete-lighter "}]

<?php
header('Content-Type: application/json');
require_once ('db.php');
$select_events  = "SELECT id, pid, title, startTime, endTime, categories FROM tl_calendar_events WHERE pid = 25 OR pid = 6";
/*$select_events  = "SELECT id, pid, title, startTime, endTime, categories FROM tl_calendar_events WHERE id = 2429";*/
$execute_events = $mysqli->query($select_events);
$data           = [];
$startIntervals = [];
$endIntervals   = [];
$title          = [];
while($row = $execute_events->fetch_array()) {
    $cssCatClass = "";
    if( $row["categories"]!= null ) {
        $category = unserialize($row["categories"]);
        foreach ($category AS $key => $value) {
            $cats = $mysqli->query("SELECT cssClass, alias FROM tl_mae_event_cat WHERE id = '" . $value . "'");
            $fetch = $cats->fetch_object();
            $cssCatClass .= $fetch->cssClass . " ";
        }
    }

    if ($row['pid'] == 6)
        $cssCatClass = 'default';

    $start         = date('Y-m-d H:i:s', $row['startTime']);
    $end           = date('Y-m-d H:i:s', $row['endTime']);

    $interval      = $start. ' till ' .$end;
    $dates         = explode(' till ', $interval);

    if(count($dates) == 2) {
        $current = $begin = new DateTime($dates[0]);
        $end = new DateTime($dates[1]);

        while($current->diff($end)->format('%a') >= 1) {

            $title[] = $row['title'];
            $nextDay = clone $current;
            $nextDay->setTime(23,59,59);

            $startIntervals [] = str_replace(' ', 'T', $current->format('Y-m-d H:i:s'));
            $endIntervals   [] = str_replace(' ', 'T', $nextDay->format('Y-m-d H:i:s'));

            $current = clone $nextDay;
            $current->setTime(0,0,0);
            $current->modify('+1 day');
        }

        $startIntervals [] = str_replace(' ', 'T', $current->format('Y-m-d H:i:s'));
        $endIntervals   [] = str_replace(' ', 'T', $end->format('Y-m-d H:i:s'));

    }

}

$j = 0;
while($j < count($endIntervals)){
    $data[] = array(
        'title' => preg_replace('/[^A-Za-z0-9\-]/', '', $title[$j]),
        'start' => $startIntervals[$j],
        'end'  => $endIntervals[$j],
        'class' => 'bg-complete-lighter '.$cssCatClass
    );
    $j++;
}
echo json_encode($data);
?>

表格

id  pid  title  startTime   endTime    categories
1   25   test   1480407061  1480500661 NULL
2   25   test2  1480327861  1480363861 NULL
3   25   test3  1480497061  1480594261 NULL

1 个答案:

答案 0 :(得分:0)

您应该再次添加:

$title[] = $row['title'];

在第二个while循环之后;

如果不这样做,则数组标题与上一个while循环中使用的数组endIntervals的元素数量不同...