Здравствуйте!
Есть 3 таблицы categories, levels, locations. У таблицы locations есть связи с таблицами categories и levels.
Таблица categories
category_key title_c
1 name1
2 name2
3 name3
...
n
Таблица levels
levels_key title_i map_i
1 level1 map1
2 level2 map2
...
n
Таблица locations
locations_key title_l about_l key_c key_i
1 name1 about1 1 1
2 name2 about2 1 1
3 name3 about3 2 2
4 name4 about4 3 2
5 name5 about5 2 1
...
n
Нужно с помощью php реализовать вывод json массива, визуальный его вид такой:
{
"mapwidth": "1000",
"mapheight": "600",
"categories": [
{
"title": "name1"
},
{
"title": "name2"
},
{
"title": "name3"
}
],
"levels": [
{
"title": "level1",
"map": "map1",
"locations": [
{
"title": "name1",
"about": "about1",
"categories": "name1"
},
{
"title": "name2",
"about": "about2",
"categories": "name1"
},
{
"title": "name5",
"about": "about5",
"categories": "name2"
}
]
},
{
"title": "level2",
"map": "map2",
"locations": [
{
"title": "name3",
"about": "about3",
"categories": "name2"
},
{
"title": "name4",
"about": "about4",
"categories": "name3"
}
]
}
]
}
На данный момент выводится следующие:
{
"mapwidth": "1000",
"mapheight": "600",
"categories": [
{
"title": "name1"
},
{
"title": "name2"
},
{
"title": "name3"
}
],
"levels": [
{
"title": "level1",
"map": "map1",
"locations": [
{
"title": "name1",
"about": "about1",
"categories": "name1"
},
{
"title": "name2",
"about": "about2",
"categories": "name1"
},
{
"title": "name3",
"about": "about3",
"categories": "name2"
},
{
"title": "name4",
"about": "about4",
"categories": "name3"
},
{
"title": "name5",
"about": "about5",
"categories": "name2"
}
]
},
{
"title": "level2",
"map": "map2",
"locations": [
{
"title": "name1",
"about": "about1",
"categories": "name1"
},
{
"title": "name2",
"about": "about2",
"categories": "name1"
},
{
"title": "name3",
"about": "about3",
"categories": "name2"
},
{
"title": "name4",
"about": "about4",
"categories": "name3"
},
{
"title": "name5",
"about": "about5",
"categories": "name2"
}
]
}
]
}
Код PHP на данный момент следующий:
<?php
$link = mysqli_connect('localhost', '', '', 'base') or die('Could not connect: ' . mysqli_error());
$query = "SELECT * FROM `categories`";
$query1 = "SELECT * FROM `locations` l INNER JOIN `categories` c ON l.category_key=c.key_c WHERE 1";
$query2 = "SELECT * FROM `levels`";
$result = mysqli_query($link, $query) or die('Query failed: ' . mysqli_error());
$result1 = mysqli_query($link, $query1) or die('Query failed: ' . mysqli_error());
$result2 = mysqli_query($link, $query2) or die('Query failed: ' . mysqli_error());
$response = [
'mapwidth'=>1000,
'mapheight'=>600,
'categories'=>[],
'levels'=>[]
];
while ($row = mysqli_fetch_object($result)) {
$response['categories'][] = [
'title'=>$row->title_c,
];
}
while ($row1 = mysqli_fetch_object($result1)) {
$response1[] = [
'title'=>$row1->title_l,
'about'=>$row1->about_l,
'category'=>$row1->title_c
];
}
while ($row2 = mysqli_fetch_object($result2)) {
$response['levels'][] = [
'title'=>$row1->title_i,
'map'=>$row1->map_i,
'locations'=>$response1
];
}
file_put_contents("chart_data.json", json_encode($response, JSON_UNESCAPED_UNICODE));
echo json_encode($response, JSON_UNESCAPED_UNICODE);
?>
Пожалуйста помогите это реализовать.