Outputting Navigation from Database?

#1
[eluser]invision[/eluser]
Hi,

I have the following architecture for my web site:
Code:
Home
About Us
- Location
- The Team
Vacancies
News
Contact

My SQL table for the Menu currently looks like this:
Code:
id | title | url | category | main
1 | home | /index.php/page/ | 1 | 1
2 | about us | /index.php/aboutus/ | 2 | 1
3 | location | /index.php/aboutus/page/location | 2 | 0
......

Now, I want to take this data from my SQL table and display it in an unordered list in my web page. N.B. Whenever it's '1' in main category, this means it's the home page of a category.

So ultimately my HTML would be something like:

Code:
<ul>
<li>Home</li>
<li>About Us
<ul>
  <li>- Location</li>
  <li>- The Team</li>
</ul>
</li>
<li>Vacancies</li>
<li>News</li>
<li>Contact</li>


Here are some snippets of code I'm already using...

Controller(page.php)
Code:
$data['cats'] = $this->MPages->getPages();

Model(MPages.php)
Code:
function getPages(){
        $data = array();
        $Q = $this->db->get('pages');
        if ($Q->num_rows() > 0){
          foreach ($Q->result_array() as $row){
                   $data[$row['id']] = $row['title'];
                  }
        }
        $Q->free_result();    
        return $data;    
    }

Nav View(nav.php)
Code:
&lt;?php
if (count($cats)){
    foreach ($cats as $id => $title){
        echo anchor("page/$slug/",$title)."<br/>";
    }
}
?&gt;

However, I've got a bit stuck at this stage.

Anyone brave enough to help me out with this?



Many thanks for any pointers.

#2
[eluser]seanloving[/eluser]
I'm not at a computer where I can test, but maybe something like this

Code:
// from your table column 'url'
$url = array( '1' => '/index.php/page/', '2' => '/index.php/aboutus/', '3' => '/index.php/aboutus/page/location' );

foreach( $url as $uri=>$string )
{
    $indented_list[$uri] = explode( '/', $string );
}

print_r( $indented_list );

then look at the output of the print_r and see there is a pattern that you can manipulate into your desired output.

#3
[eluser]theprodigy[/eluser]
Here is what I typically use. The database table layout is a little different than what you currently have. Instead of:
Quote:id | title | url | category | main
it uses:
Quote:id | parent_id | name | link | new_page | active
where:
Quote:id = menu item id
parent_id = id of parent menu item (for multi-level menus) (recursive foreign key)
name = display name for menu
link = url
new_page = boolean on whether the menu item opens in a new page
active = boolean on whether that menu item is to be shown (soft delete)
NOTE: This creates everything from opening ul to closing ul. The opening ul contains id='nav'. If you read the code, you should be able to figure out where and how to change it.
Code:
function get_menu()
{
    $this->db->select('id,parent_id,name,link,new_page');
    $this->db->from('menu');
    $this->db->where('active','1');
    $menu = $this->db->get();
        
    $menuData = array(
        'items' => array(),
        'parents' => array()
    );
    $menu_item_id=0;
    foreach($menu->result_array() as $menuItem)
    {
        $menuData['items'][$menuItem['id']] = $menuItem;
        $menuData['parents'][$menuItem['parent_id']][] = $menuItem['id'];
        
    }
    return $this->buildMenu(0, $menuData);
}
    
function buildMenu($parentId, $menuData)
{
    $html = '';
        
    if (isset($menuData['parents'][$parentId]))
    {
        if($parentId==0)
        {
            $html = "\n<ul id='nav'>\n";
        }
        else
        {
            $html = "\n<ul>\n";
        }
        foreach ($menuData['parents'][$parentId] as $itemId)
        {
            $target = "";
            if($menuData['items'][$itemId]['new_page'] == 1)
            {
                $target = " target='_blank'";
            }
            $html .= '<li><a href="'.$menuData['items'][$itemId]['link'].'">' . $menuData['items'][$itemId]['name'].'</a>';
            // find child items recursively
            $html .= $this->buildMenu($itemId, $menuData);

            $html .= '</li>'."\n";
        }
        $html .= '</ul>';
    }
    return $html;
}

#4
[eluser]invision[/eluser]
Many thanks for your help guys, especially the_prodigy.

I'm going to give this good look over shortly and see how I go.

Again, thanks so much for helping out with this.
The project site I'm building is coming along really well. I'm now working on the backend CMS part Smile


Thanks

#5
[eluser]zlatiborac[/eluser]
This
Code:
function buildMenu
saved my life. Especially the part with html .=
I am so confused with what I can pass through $data[];
Many thanks...


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.