Using Foreach to Create Submenus

I have two functions that I'm working on that interact with each other, and at this point I'm trying to fix part of the submenu.

My menu is created using

public function CreateNavigation() {
            global $db;
            $query = <<<SQL
            SELECT id,name
            FROM pages
            WHERE enabled = :enabled
            AND main = :main
SQL;
            $resource = $db->sitedb->prepare( $query );
            $resource->execute( array (
            ':enabled'  =>  1,
            ':main'     =>  1,
            ));
                foreach($resource as $menu){
                    $this->parentid = $menu['id'];
                    echo "<li><a href='viewPage.php?id=".$menu['id']."'>".$menu['name']."</a>
                    ".self::GetSublinks()."</li>";
                }

    }

And then GetSublinks takes over to determine the dropdown menu.

    public function GetSublinks() {
        global $db;
        $query = <<<SQL
        SELECT id,name
        FROM pages
        WHERE parentid = :parentid
        AND enabled = :enabled
SQL;
        $resource = $db->sitedb->prepare( $query );
        $resource->execute( array (
        ':parentid' => $this->parentid,
        ':enabled'  => 1,
        ));
        $row_count = $resource->rowCount();
        if($row_count >= 1) {
            return "<ul>";
            foreach($resource as $row){
                return "<li><a href='viewpage.php?id=".$row['id'].">".$row['name']."</a></li>"
            }
            return "</ul>";
        }
    }

Now the main menu by itself works fine, Even sublinks work fine. They check to make sure and change the main link to show that she has children, if that happens, but where I run into a problem, inside the children she will show only the last of the links, so I cannot have more than one drop-down list because of this. I also tried changing my foreach within sublinks to

while($row = $resource->fetch(PDO::FETCH_ASSOC))

To no avail. My menu works fine without a dropdown, but a dropdown would be nice to add.

var_dump ($ resource) :

object(PDOStatement)#7 (1) { ["queryString"]=> string(86) " SELECT id,name FROM pages WHERE parentid = :parentid AND enabled = :enabled" } object(PDOStatement)#7 (1) { ["queryString"]=> string(86) " SELECT id,name FROM pages WHERE parentid = :parentid AND enabled = :enabled" } 

var_dump ($ row) :

array(2) { ["id"]=> string(1) "2" ["name"]=> string(10) "Create New" } array(2) { ["id"]=> string(1) "3" ["name"]=> string(14) "Delete Listing" } 
+4
2

return "<ul>";

<ul>, foreach,

return ¶ , return . return eval() script.

if($row_count >= 1) {
       $sub_menu = "<ul>";
       foreach($resource as $row){
            $sub_menu .= "<li><a href='viewpage.php?id=".$row['id'].">".$row['name']."</a></li>"
        }
         return $sub_menu ."</ul>";
  }
+1

, . :

    if($row_count >= 1) {
        $subMenu = "";
        $subMenu .= "<ul>";
        foreach($resource as $row){
            $subMenu .= "<li><a href='viewpage.php?id=".$row['id'].">".$row['name']."</a></li>"
        }
        $subMenu .= "</ul>";
        return $subMenu;
    }

.

+3

All Articles