Welcome Guest, Not a member yet? Register   Sign In
Outputting Navigation from Database?


I have the following architecture for my web site:
About Us
- Location
- The Team

My SQL table for the Menu currently looks like this:
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:

<li>About Us
  <li>- Location</li>
  <li>- The Team</li>

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

$data['cats'] = $this->MPages->getPages();

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'];
        return $data;    

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

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

Anyone brave enough to help me out with this?

Many thanks for any pointers.

I'm not at a computer where I can test, but maybe something like this

// 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.

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
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.
function get_menu()
    $menu = $this->db->get();
    $menuData = array(
        'items' => array(),
        'parents' => array()
    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]))
            $html = "\n<ul id='nav'>\n";
            $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;

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


function buildMenu
saved my life. Especially the part with html .=
I am so confused with what I can pass through $data[];
Many thanks...

Theme © iAndrew 2016 - Forum software by © MyBB