0

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.

Javascript with PHP Foreach

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!

Born2Discover
  • 133
  • 13

1 Answers1

0

As your final structure should not be an indexed array, but should be associative (an object in JS), don't use $json[] =, but $json[date] =.

So replace:

$json[] = array(
    date("Y-m-d", strtotime($e[date])) => array(
    'number'=> $e[number],
    'url'=> $e[url],
    'status'=> ($e[status] == 1 ? true : false)
    ));

... with:

$json[date("Y-m-d", strtotime($e[date]))] = array(
    'number'=> $e[number],
    'url'=> $e[url],
    'status'=> ($e[status] == 1 ? true : false)
);
trincot
  • 317,000
  • 35
  • 244
  • 286
  • Hello, I implemented the suggested method, and it indeed generates the correct format I need, but it only lists the last record of the foreach. – Born2Discover Oct 10 '19 at 21:20
  • UPDATE: Discovered an issue with my SQL code upon retrying, though it still for some reason only shows a few results of a table I know has many more. However, the result is definitely what I am looking for, I believe I can figure it out from here. Thank you so much. – Born2Discover Oct 10 '19 at 21:28
  • UPDATE: The issue of the records not showing was related to many having the same date, so this is certainly an educational understanding of how grouping works in arrays. Will need to see about splitting similar dates. – Born2Discover Oct 10 '19 at 21:32