Im going to leave the details to yourself, but it will be something like this. Recursive is the keyword:
function getMenu($parent=0, $depth=0){
$menu = "<ul>"; // each section gets wrapped in UL
// Select and query to get only the direct childs from $parent:
$qItems = "SELECT id, page_name FROM pages WHERE parent=".$parent;
$sItems = mysqli_query($conn, $qItems);
// This will be the magic part:
while($fItems = $sItems->fetch_assoc() ){
$menu.= '<li>';
$menu.= str_repeat('-', $depth).' '.$fItems['page_name'];
$menu.= getMenu( $fItems['id'], $depth+1); // <- this is the magic! This will get the childs of this item
$menu.= '</li>';
}
$menu.= "</ul>"; // each section gets wrapped in UL
return $menu; // return it
}
echo getMenu(); // Do something with it :)
I've added the $depth
in this example so the dashes get place, to show how you can find your way into recursiveness
Edit: Small but important note: This will go on endlessly if you keep adding childs to childs to ..., no need to update the code. That's one of the great powers of recursiveness