Efficiently creating a menu consisting of multi-level structure I was asked to modify existing code to allow multi-level page structure. Each page can have one parent, and one page can have multiple children.

I will present following code. My question is is there a more efficient way of doing this? I am hard-coding the number of levels that are reachable. 

Is there easier way to do  this automatically without hard coding? Recursion perhaps?

function createMenu($parents) {
    foreach ($parents as $parent) {
        $parentName = $this-&gt;getPagefromID($parent);
        $kids = $this-&gt;areThereKids($parent);

        if ($kids == "yes") {
            echo "&lt;ul class=\"children\"&gt;\n";
            $query = "SELECT * FROM pages";
            $result = mysql_query($query) or die('Query failed: ' . mysql_error());
            while ($row = mysql_fetch_assoc($result)) {
                $thisparent = $row['Parent'];
                $name = $row['Name'];
                $id2 = $row['ID'];
                if ($thisparent == $parent) {
                    echo "&lt;li&gt;&lt;a href=\"edit.php?id=$id2\"&gt;" . $name . "&lt;/a&gt;&lt;/li&gt;\n";
                    if($this-&gt;areThereKids($id2) == "yes") {
                        $children = $this-&gt;getMyKids($id2);
                        foreach($children as $kid) {
                            $info = $this-&gt;getPersonInformation($kid);
                            $kidid = $info[0]["ID"];
                            echo "&lt;li&gt;&lt;a href=\"edit.php?id=$kidid\"&gt;" . $info[0]['Name'] . "&lt;/a&gt;&lt;/li&gt;\n";
                            // more sub
                            if($this-&gt;areThereKids($kidid) == "yes") {
                                $children1 = $this-&gt;getMyKids($kidid);
                                foreach($children1 as $kid1) {
                                    $info1 = $this-&gt;getPersonInformation($kid1);
                                    $kidid1 = $info1[0]['ID'];
                                    echo "&lt;li&gt;&lt;a href=\"edit.php?id=$kidid1\"&gt;-" . $info1[0]['Name'] . "&lt;/a&gt;&lt;/li&gt;\n";
                                    if($this-&gt;areThereKids($kidid1) == "yes") {
                                        $children2 = $this-&gt;getMyKids($kidid1);
                                        foreach($children2 as $kid2) {
                                            $info2 = $this-&gt;getPersonInformation($kid2);
                                            $kidid2 = $info2[0]['ID'];
                                            echo "&lt;li&gt;&lt;a href=\"edit.php?id=$kidid2\"&gt;--" . $info2[0]['Name'] . "&lt;/a&gt;&lt;/li&gt;\n";
                                            // No need for 6 levels.
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            echo "&lt;/ul&gt;\n";
        }
        echo "&lt;/li&gt;\n";
    }
}
@hjpotter92 makes a good point: mysql_* is deprecated and should be replaced with mysqli_* or PDO. It's probably easier to replace it with the former, but it may help to read this article comparing mysqli and PDO.
$this-&gt;areThereKids() is a terrible method name. It's even worse that it returns "yes" or some other value. Refactor this to $this-&gt;hasKids() (or better yet, $this-&gt;hasChildren()), returning either boolean true or false.


Similarly, $this-&gt;getMyKids() could be better named $this-&gt;getChildren(), but this is not as egregious as the point above.

or die(mysql_error()) is tolerable in development code, but should be avoided in production code. Instead, prefer to print a user-friendly error or apologetic message, and log the details separately.
You are correct in that your code lends itself naturally to recursion. I'm not interested in writing the recursive function for you, but notice in particular that in every level of nesting,


you use the same template for output.
you have the variables $info and $id.
if there are children, you store them in a variable $children and you iterate through them, using the same template and variables.


I welcome you to try implementing the recursive function on your own, keeping the above three points in mind. When you've done so, you can submit your revised code here for another review!