I have been looking and trying many different ways to create the JSON below from a postgres table using PHP, but did not work, specially to add the "group". Group should be added when there are different date, so it would create groups by date, as the example below. Does anyone know how can I do it?
EXPECTED:
"t": [
{
"group": "2015-03-25",
"list": [{
"t": 1,
"titulo": "Pages - Multi-Purpose Admin Template Revolution Begins here!",
"to": ["David Nester", "Jane Smith"],
"time": "5 min. atrás",
"datetime" : "Today at 1:33pm",
"from": "David Nester",
"icone": 1
}, {
"t": 2,
"titulo": "Your site has some very imaginative animation /movement, especially the Sluggo! ",
"to": ["Anne Simons"],
"time": "45 min. atrás",
"datetime" : "Today at 1:33pm",
"from": "Anne Simons",
"icone": 2
}, {
"t": 3,
"titulo": "Recently ordered a new pair of soccer cleats from your website on June 21",
"to": ["Herald Menster"],
"time": "13:33",
"datetime" : "Today at 1:33pm",
"from": "David Nester",
"icone": 1
}, {
"t": 4,
"titulo": "Everything here, Made Just for you :)",
"to": ["John Doe"],
"time": "11:23",
"datetime" : "Today at 11:23am",
"from": "David Nester",
"icone": 3
}, {
"t": 5,
"titulo": "Simplicity is the ultimate sophistication",
"to": ["John Doe", "Anne Simons"],
"time": "22:33",
"datetime" : "Today at 10:33pm",
"from": "David Nester",
"icone": 2
}]
}, {
"group": "2015-03-24",
"list": [{
"t": 6,
"titulo": "Good design is obvious. Great design is transparent",
"to": ["John Doe", "Anne Simons"],
"time": "13:33",
"datetime" : "Today at 1:33pm",
"from": "David Nester",
"icone": 1
}, {
"t": 7,
"titulo": "Your site has some very imaginative animation /movement, especially the Sluggo! ",
"to": ["Anne Simons"],
"time": "45 mins ago",
"datetime" : "Today às 13:33",
"from": "Anne Simons",
"icone": 2
}, {
"t": 8,
"titulo": "Aliquam est tellus, fringilla egestas fermentum quis",
"to": ["John Doe", "Anne Simons"],
"time": "13:33",
"datetime" : "Today at 1:33pm",
"from": "David Nester",
"icone": 2
}, {
"t": 9,
"titulo": "Aliquam est tellus, fringilla egestas fermentum quis",
"to": ["John Doe", "Anne Simons"],
"time": "13:33",
"datetime" : "Today at 1:33pm",
"from": "David Nester",
"icone": 1
}
CURRENT CODE:
With this code below I get the following result, but it never add different "group", just add the first one:
echo '{ "t": [';
while ($row = pg_fetch_array($result, null, PGSQL_ASSOC)) {
$data["group"] = $row["datetime"];
$data["list"][] = $row;
};
echo json_encode($data);
echo "]}";
WRONG RESULT:
{
"t": [
{
"group": "2014-09-22",
"list": [
{
"t": "133640",
"titulo": "Some problem",
"to": "Erik",
"time": "9:30",
"datetime": "2014-09-22",
"from": "Julian",
"icone": "1"
},
{
"t": "133641",
"titulo": "Problems",
"to": "Robert",
"time": "9:30",
"datetime": "2014-09-22",
"from": "Julian",
"icone": "1"
}
]
}
]
}