sam sam sam sam - 7 days ago 7
PHP Question

Data is missing from json response in PHP

What I am doing is, listing events from table on calendar. Some events ending time is more than 24 hour so I am splitting dates and listing as separate event on calendar. At-present my problem is title is not showing in json response(Please check the response

"title":"test" "title":""
).

What I need is, based on id 1:

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


Eg of id 1:

Table id= 1: It’s startTime is
14804070619 (2016-11-29 08:11:01)
and endTime is
1480500661 (2016-11-30 10:11:01)
.

After splitting 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"


Response of 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 "}]


code

<?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);
?>


table

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

Answer

You should add a second time :

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

just after the second while loop;

if you don't, the array title has not the same number of elements as the array endIntervals used in the last while loop...

Comments