<?php
$query = "SELECT
c.id AS categories_id,
c.category AS categories_category,
sc.id AS subcategories_id,
sc.subcategory AS subcategories_subcategory,
sc.category AS subcategories_category
FROM subcategories sc
RIGHT JOIN categories c ON c.id = sc.category";
$result = mysqli_query($link, $query) or die (mysqli_error($link));
for ($data = []; $row = mysqli_fetch_assoc($result); $data[] = $row);
var_dump($data);
$content = '';
foreach ($data as $elem) {
$content .= "<li class=\"sub\">
<a href=\"\">$elem[categories_category]</a>
</li>";
if ($elem['categories_id'] == $elem['subcategories_category']) {
$content .= "<ul>
<li class=\"down_item\">
<a href=\"\">$elem[subcategories_subcategory]</a>
</li>
</ul>";
}
}
?>
function getTree($dataset) {
$tree = array();
foreach ($dataset as $id => &$node) {
//Если нет вложений
if (!$node->parent_id){
$tree[$id] = &$node;
}else{
//Если есть потомки то переберем массив
$dataset[$node->parent_id]->childs[$id] = &$node;
}
}
return $tree;
}