Pages system PHP/SQL I'm working on a CMS, currently on being able to create static pages, manage menus etc.
so.. I have This function that reads from the DB and finds all the static pages, and their position in the menu.
I want to know what Do you think. 

function update_menu(&amp;$pdo, $filepath){
$qry = "SELECT * FROM pages WHERE top = 1";
$exec = $pdo-&gt;query($qry);
$string = "&lt;ul&gt;";
while($row=$exec-&gt;fetch(PDO::FETCH_ASSOC)){
    $string.= "&lt;li&gt;&lt;a href='?page=".$row['id']."'&gt;".$row['title']."&lt;/a&gt;";
        $qry = "SELECT * FROM pages WHERE parent = ?";
        $child_ex = $pdo-&gt;prepare($qry);
        $child_ex-&gt;bindValue(1, $row['id'], PDO::PARAM_INT);
        if($child_ex-&gt;execute()){
            $i=0;
            while($child=$child_ex-&gt;fetch(PDO::FETCH_ASSOC)){
                if($i==0){
                    $string.="&lt;ul&gt;";
                }
                $string.="&lt;li&gt;&lt;a href='?page=".$child['id']."'&gt;".$child['title']."&lt;/a&gt;";
                $i++;
            }
            if($i != 0)
                $string.="&lt;/ul&gt;";
        }
    $string.="&lt;/li&gt;";
}
$string.="&lt;/ul&gt;";
file_put_contents($filepath, $string);


};

Output html: 

&lt;ul&gt;&lt;li&gt;&lt;a href='?page=1'&gt;Page1&lt;/a&gt;&lt;ul&gt;&lt;li&gt;&lt;a href='?page=4'&gt;append1&lt;/a&gt;&lt;li&gt;&lt;a href='?page=5'&gt;append2&lt;/a&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;&lt;a href='?page=2'&gt;page2&lt;/a&gt;&lt;ul&gt;&lt;li&gt;&lt;a href='?page=6'&gt;append1b&lt;/a&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;&lt;a href='?page=3'&gt;page3&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;
I think it overall is good, but there's some minor changes that could still quicken this script, namely using fetchAll and foreach instead of fetch and while

using fetchAll will get the values quicker then fetch on every single row. also foreach will prevent you from having to check if you have results, it just won't run if there are not.

also, you prepare the statement multiple times in the huge loop, instead of preparing it outside of the loop and execute() it multiple times

function update_menu(&amp;$pdo, $filepath){
    $qry = "SELECT * FROM pages WHERE top = 1";
    $exec = $pdo-&gt;query($qry)-&gt;fetchAll(PDO::FETCH_ASSOC);
    $qryChild = $pdo-&gt;prepare("SELECT * FROM pages WHERE parent = ?");
    $string = '&lt;ul&gt;' . "\n";
    foreach($exec as $row)){
        $string.= "&lt;li&gt;&lt;a href='?page=".$row['id']."'&gt;".$row['title'].'&lt;/a&gt;' . "\n";
        $qryChild-&gt;execute($row['id']);
        $childs = $qryChild-&gt;fetchAll(PDO::FETCH_ASSOC);
        $i=0;
        foreach($childs as $child){
            if($i==0){
                $string.='&lt;ul&gt;' . "\n";
            }
            $string.="&lt;li&gt;&lt;a href='?page=".$child['id']."'&gt;".$child['title'].'&lt;/a&gt;' . "\n";
            $i++;
        }
    if($i != 0) $string.='&lt;/ul&gt;' . "\n";
    $string.='&lt;/li&gt;' . "\n";
    }
$string.='&lt;/ul&gt;' . "\n";
file_put_contents($filepath, $string);


also, adding some "\n" will give some better readability to you html code