function GetChildren($m_id){
global $db, $tbl_main;
static $aux;
$SQL="SELECT m_id, m_name, m_parent_id
FROM $tbl_main
WHERE m_parent_id=$m_id
ORDER BY m_sort";
$query=$db->Execute($SQL);
while ($array = $query->FetchRow()) {
$children[] = array('id' => $array['m_id'], 'name' => $array['m_name']);
}
if ($children){
foreach ($children as $value){
// print_r ($value);
$aux[] = $value;
print_r($aux);
GetChildren($value['id']);
}
}
return $children;
}
print '<pre>';
GetChildren($root_id);
// print_r(GetChildren($root_id));
print '</pre>';
function GetChildren($m_id){
global $db, $tbl_main;
$SQL="SELECT m_id, m_name, m_parent_id
FROM $tbl_main
WHERE m_parent_id=$m_id
ORDER BY m_sort";
$query=$db->Execute($SQL);
while ($array = $query->FetchRow()) {
$children[] = array('id' => $array['m_id'], 'name' => $array['m_name']);
}
if ($children){
foreach ($children as $value){
$aux[] = GetChildren($value['id']);
}
}
return $aux[];
}
print '<pre>';
// print_r(GetChildren($root_id));
print '</pre>';
<?php
function getChildren($m_id, $m_name) {
global $db, $tbl_main;
$SQL =
"SELECT m_id, m_name, m_parent_id FROM $tbl_main
WHERE m_parent_id = $m_id ORDER BY m_sort";
$query = $db->query($SQL);
// получим массив, в котором будет дерево элементов в иерархическом порядке
$tree = [
'id' => $m_id,
'name' => $m_name,
'children' => [],
];
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
$tree['children'][] = getChildren($row['m_id'], $row['m_name']);
}
return $tree;
}
print '<pre>';
print_r(getChildren($root_id, $root_name));
print '</pre>';