1

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:

mydatabase

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

11

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.