I am trying to build an un-oredered list menu tree from my database in PHP and MySQL from here
I have an array of page objects I am returning from the db. Each page object has parent_id attribute, which is set to null if it doesn't have a parent. Here's what the page objects look like:
PHP Handling
$menu = mysqli_query($conn, "SELECT * FROM kategori ORDER BY id_kategori DESC");
while($row=mysqli_fetch_assoc($menu)) {
}
function has_children($rows,$id) {
foreach ($rows as $row) {
if ($row['parent'] == $id)
return true;
}
return false;
}
function build_menu($rows,$parent=0)
{
$result = "<ul>";
foreach ($rows as $row)
{
if ($row['parent'] == $parent){
$result.= "<li>{$row['nama_kategori']}";
if (has_children($rows,$row['id_kategori']))
$result.= build_menu($rows,$row['id_kategori']);
$result.= "</li>";
}
}
$result.= "</ul>";
return $result;
}
echo build_menu($menu);
This isn't outputting the correct results for me, can anyone help me