<?php
header('Content-Type: application/json; charset=utf-8');
/*
* @Purpose: This file is about making JSON data
* @author : goldsky
* @date : 20100210
*/
// Database settings (localhost? username? password?)
include_once('dbonfig.inc.php');
$conn = mysql_connect($database_server, $database_user, $database_password);
if (!$conn ) {
die('Not connected : ' . mysql_error());
}
// select db
$dbconn = mysql_select_db($dbase,$conn);
if (!$dbconn ) {
die ('Can\'t select db : ' . mysql_error());
}
// generating event attributes inside a function
function eventAtt() {
$eventQuery=mysql_query('SELECT * FROM tableName') or die (mysql_error());
while ($row = mysql_fetch_array($eventQuery)) {
$date = explode("-", $row['date']); // in my case, $date is stored as yyyy-mm-dd in MySQL table.
$phpmakedate = mktime(0, 0, 0, $date[1], $date[2], $date[0]);
// ------------ optionally with "end" date ------------
if ($row['enddate']== NULL || $row['enddate'] == '0000-00-00') {
$phpenddate = NULL; // to skip latestStart date
$durationEvent = FALSE; //JSON
}
else {
$enddate = explode("-", $row['enddate']);
$phpmakeenddate = mktime(0, 0, 0, $enddate[1], $enddate[2], $enddate[0]);
$phpenddate = date("r",$phpmakeenddate);
$durationEvent = TRUE; //JSON
}
// ------------ create the array here ------------
$eventAtts[] = array (
'start' => date("r",$phpmakedate),
'end' => $phpenddate,
'durationEvent' => $durationEvent,
'description' => $row['description']
);
}
mysql_free_result($eventQuery);
return $eventAtts;
}
////////////////////////////////////////////
// //
// TIMELINE'S JSON DATA //
// //
////////////////////////////////////////////
//
$json_data = array (
//Timeline attributes
'wiki-url'=>'http://simile.mit.edu/shelf',
'wiki-section'=>'Simile Cubism Timeline',
'dateTimeFormat'=>'Gregorian', //JSON!
//Event attributes
'events'=> eventAtt() // <---- here is the event arrays from function above.
);
$json_encoded=json_encode($json_data);
echo $json_encoded;
?>