Building a JSON Array from a MySQL Database
Introduction
Creating JSON arrays from MySQL databases is essential for dynamic web applications. This guide provides step-by-step instructions on how to design a JSON array that adheres to the specified format, ensuring compatibility with fullcalendar.
Implementation
The following modified code snippet retrieves data from a MySQL database and populates a JSON array in the required format:
$year = date('Y'); $month = date('m'); $mysql_query = "SELECT * FROM events"; $result = mysql_query($mysql_query); $json_array = array(); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $event_id = $row['event_id']; $title = $row['title']; $start = "$year-$month-{$row['day']}"; if (!empty($row['end_day'])) { $end = "$year-$month-{$row['end_day']}"; } else { $end = null; } $url = $row['url']; $event_array = array( 'id' => $event_id, 'title' => $title, 'start' => $start, 'end' => $end, 'url' => $url ); array_push($json_array, $event_array); } echo json_encode($json_array);
This code fetches event data from a MySQL table and constructs a JSON array compatible with fullcalendar. Each event object contains the required properties:
End Result
The JSON array generated will have the following structure:
[ { "id": 111, "title": "Event1", "start": "2023-03-10", "url": "http://yahoo.com/" }, { "id": 222, "title": "Event2", "start": "2023-03-20", "end": "2023-03-22", "url": "http://yahoo.com/" } ]
This JSON array can be directly consumed by fullcalendar or other applications requiring dynamic event data.
The above is the detailed content of How to Build a JSON Array from a MySQL Database for Fullcalendar Integration?. For more information, please follow other related articles on the PHP Chinese website!