I have looked through Stackoverflow for the last several hours, none of the questions that are similar answer what I am looking for. There was one, however the chosen answer was nothing I didn't already try.
I am needing to create a JavaScript variable array dynamically from the database. I have no problem generating JSON arrays which are useful in other areas of my site however the format required for the plugin responsiveCalendar requires a unique format I cannot mimic.
The code of mine outputs this type of response
$(".responsive-calendar").responsiveCalendar({
time: '<?php echo $today; ?>',
events: {
"2014-04-30": {"number": 1, "badgeClass": "badge-warning", "url": "http://w3widgets.com/responsive-calendar"},
"2013-04-26": {"number": 1, "badgeClass": "badge-warning", "url": "http://w3widgets.com"},
"2013-05-03": {"number": 1, "badgeClass": "badge-error"},
"2013-06-12": {}}
});
The script I am using is the following
$sql = $conn->prepare('SELECT date,total,url,status FROM SQLTABLE ORDER BY date DESC');
$sql->execute();
$json = [];
$events = $sql->fetchAll(PDO::FETCH_ASSOC);
foreach ($events as $e) {
$json[] = array(
date("Y-m-d", strtotime($e[date])) => array(
'number'=> $e[number],
'url'=> $e[url],
'status'=> ($e[status] == 1 ? true : false)
));
}
$events = json_encode($json, JSON_UNESCAPED_SLASHES);
echo $events;
$conn->close();
This is the result I am getting
[{
"2017-09-05": {
"number": 10,
"url": "http",
"active": true
}
}, {
"2017-09-05": {
"number": 7,
"url": "http",
"active": false
}
},
...
{
"2017-09-05": {
"number": 15,
"url": "http",
"active": false
}
}, {
"2017-09-05": {
"number": 1,
"url": "http",
"active": false
}
}]
I am trying to accomplish this (below) which would then be assigned to the object variable within the plugin called events. I did try it with square brackets in place such as how some Ajax data passes are made, but that broke it so not sure what syntax rule is at play here.
{
"2017-09-05": {
"number": 10,
"url": "http",
"active": true
},
"2017-09-05": {
"number": 7,
"url": "http",
"active": false
},
...
"2017-09-05": {
"number": 15,
"url": "http",
"active": false
},
"2017-09-05": {
"number": 1,
"url": "http",
"active": false
}
}
So to summarize this entails I need no square brackets, and notice that the first part of each event stands alone without any identifier such as number, url, status, etc. It is just the date field. In my amateur understanding, the date field serves as I believe the index value of the sub array.
Thank you in advance, really appreciate the StackOverflow community!