Page 1 of 1

Infinite Submenu

Posted: Tue Sep 18, 2012 2:16 pm
by EcazS
I've been improving my CMS over time and I would like to add submenus, infinite amounts of it...
Problem is, I have no idea how I would go about this with my current code, I understand I need to use a recursive method but that's about it.

getter.class.php
public function NavList() {
    global $mysql;
            
    $mysql->query("SELECT `name`, `url`, `index`, `id` FROM `pageData` ORDER BY `id` ASC");
            
    $nav_data = array();
	while ($mysql->fetch($row)){
        $nav_data[$row["name"]] = array(
            "url"   => $row["url"],
            "name"  => current(explode(".", $row["name"])),
	    "index"	=> $row["index"],
	    "id"	=> $row["id"]
        );
    }
    return $nav_data;
}
setter.class.php
$nav_data  = $this->get->NavList();
public.php
<ul>
<?php
	foreach($nav_data as $nav) {
?>
	<li>
	<a href="<?= $nav["url"]; ?>" 
	class="<?php if(isset($_GET["p"]) && ($_GET["p"] == $nav["url"])) { echo "active"; }
	elseif(!isset($_GET["p"]) && (empty($_GET["p"]) && ($nav["index"] == 1))) 
        { echo "active"; } ?>"><?= $nav["name"]; ?></a></li>
<?php
	}
?>
</ul>
So, if you have some tips or some ideas on how I would create submenus I would greatly appreciate it!
Preferably without having a lot of code in the public.php file.

Re: Infinite Submenu

Posted: Sat Sep 22, 2012 10:56 pm
by jacek
I would want to avoid a recursive function that does a query for the sake or performance. If you give each menu an ID and a field for the parent ID then you can select the whole data set in one query and than expand it out into a an array as deep as you need.

Re: Infinite Submenu

Posted: Sat Sep 22, 2012 11:06 pm
by EcazS
Yes... I could do that...

Re: Infinite Submenu

Posted: Sat Sep 22, 2012 11:07 pm
by jacek
EcazS wrote:Yes... I could do that...
Get to it !!