Infinite Submenu

Ask about a PHP problem here.
Post Reply
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Infinite Submenu

Post 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.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Infinite Submenu

Post 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.
Image
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Re: Infinite Submenu

Post by EcazS »

Yes... I could do that...
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Infinite Submenu

Post by jacek »

EcazS wrote:Yes... I could do that...
Get to it !!
Image
Post Reply