I am using a plugin that wants to be fed an array like this in javascript:
var data = [
{
"id": 1,
"name": "University1",
"list": [
{"id": 1, "name": "Dorms", "list":
[
{"id": 1, "name": "Dorm1"},
{"id": 2, "name": "Dorm2"}
]
},
{"id": 2, "name": "Off-Campus", "list":
[
{"id": 1, "name": "North Campus"},
{"id": 2, "name": "East Campus"}
]
}
]
},
{
"id": 2,
"name": "University2",
"list": [
{"id": 1, "name": "Dorms", "list":
[
{"id": 1, "name": "Dorm1"},
{"id": 2, "name": "Dorm2"}
]
},
{"id": 2, "name": "Off-Campus", "list":
[
{"id": 1, "name": "North Campus"},
{"id": 2, "name": "East Campus"}
]
}
]
}
];
My data for the array is in an SQL database. I am having trouble forming this multi-dimensional array in php and/or passing it with AJAX.
My javascript/jquery:
var locationsArray;
$.post(
'ajax/locationDropdown.php',
{
//NO DATA THIS TIME
},
function (response) {
console.log(response);
parseResponse = $.parseJSON(response);
var locationsArray = $.map(parseResponse, function(value, index) {
return [value];
});
console.log(locationsArray);
}
);
My php:
<?php
include 'databaseConnection.php';
$sqlLD1 = '
SELECT DISTINCT school
FROM timeBlocks
ORDER BY school ASC;
';
if (!$resultLD1 = $connection->query($sqlLD1)) {
die ('There was an error running the queryLD1 [' . $connection->error . ']');
}
$locationArray = array(
'id'=>array(),
'name'=>array(),
'list'=>array(
'id'=>array(),
'name'=>array(),
'list'=>array(
'id'=>array(),
'name'=>array()
)
)
);
$i=0;
while ($rowLD1 = $resultLD1->fetch_assoc()) {
$school = $rowLD1["school"];
$locationArray[$i][name] = $school;
$sqlLD2 = '
SELECT DISTINCT timeBlockLocation
FROM timeBlocks
WHERE school = "'.$rowLD1["school"].'"
ORDER BY timeBlockLocation ASC;
';
if (!$resultLD2 = $connection->query($sqlLD2)) {
die ('There was an error running the queryLD2 [' . $connection->error . ']');
}
$j=0;
while ($rowLD2 = $resultLD2->fetch_assoc()) {
$timeBlockLocation = $rowLD2["timeBlockLocation"];
$locationArray[$i][$j][name]=$timeBlockLocation;
$sqlLD3 = '
SELECT DISTINCT timeBlockSubLocation
FROM timeBlocks
WHERE school = "'.$rowLD1["school"].'"
AND timeBlockLocation = "'.$rowLD2["timeBlockLocation"].'"
ORDER BY timeBlockSubLocation ASC;
';
if (!$resultLD3 = $connection->query($sqlLD3)) {
die ('There was an error running the queryLD2 [' . $connection->error . ']');
}
$k=0;
while ($rowLD3 = $resultLD3->fetch_assoc()) {
$timeBlockSubLocation = $rowLD3["timeBlockSubLocation"];
$locationArray[$i][$j][$k][name]=$timeBlockSubLocation;
$k++;
}
$j++;
}
$i++;
}
echo json_encode($locationArray);
?>
This is resulting in an array that looks like this:
{
"0": {
"0": {
"0": {
"name": "All Locations"
},
"name": "Off Campus"
},
"1": {
"0": {
"name": "Dorm1"
},
"1": {
"name": "Dorm2"
}
"name": "Dorms"
},
"name": "University1"
},
"1": {
"0": {
"0": {
"name": "All Locations"
},
"name": "Off-Campus"
},
"1": {
"0": {
"name": "Dorm1"
},
"name": "Dorms"
}
"name": "University2"
},
"id": [],
"name": [],
"list": {
"id": [],
"name": [],
"list": {
"id": [],
"name": []
}
}
}