Jump to content

PHP CodeIgniter Dynamic navigation


jaapjolman

Recommended Posts

Controller:

 

<?php
function gui()
{
	$nav = $this->site_model->navigation();
	$page = $this->site_model->page_config();
	$this->load->view('site/site',array('page' => $page, 'nav'=> $nav));	
}
?>

 

Model:

 

<?php

function navigation()
{	

	// Parent Menu

	$this->db->order_by('order', 'asc');
        $this->db->where('subid', '0');
        $query = $this->db->get('navigation');

	$i = 0;

	foreach($query->result() as $row)
	{

		$parentitem[$i]['id'] = $row->id;
		$parentitem[$i]['link'] = $row->link;
		$parentitem[$i]['title'] = $row->title;

		// Sub Menu

		$this->db->order_by('order', 'asc');
		$this->db->where('subid', $parentitem[$i]['id']);
		$subquery = $this->db->get('navigation');

		$sub = 0;

		foreach($subquery->result() as $row)
		{

			$subitem[$sub]['id'] = $row->id;
			$subitem[$sub]['link'] = $row->link;
			$subitem[$sub]['title'] = $row->title;

			$sub++;

		}

		$i++;
	}

	$nav;
	$nav = array();

	$nav['parent'] = $parentitem;
	$nav['sub'] = $subitem;

	return $nav;
}
?>

 

View:

 

<?php
foreach($nav['parent'] as $parent)
	{
		echo '<h3><a name="'.$parent['link'].'">'.$parent['title'].'</a></h3>';
		echo '<div>';
		echo '<ul>';
		foreach($nav['sub'] as $subitem)
		{

			echo '<li><a name="'.$subitem['link'].'">'.$subitem['title'].'</a></li>';

		}
		echo '</ul>';
		echo '</div>';

	}
?>

 

the script works for the parent navigational links but not the sub links it keeps repeating the last subitem from the db

 

I only posted the relivant portion of code

Link to comment
Share on other sites

  • 2 weeks later...
  • 5 years later...

model : -->

 

function MenuSub(){
        $this->db->select('*');
        $this->db->where('page_parent_id', '0');
        $query = $this->db->get($this->table);
        
        $i = 0;
        $sub = 0;
        foreach($query->result() as $row)
        {
        
            $parentitem[$i]['page_id'] = $row->page_id;
            $parentitem[$i]['page_menutitle'] = $row->page_menutitle;
            $parentitem[$i]['page_parent_id'] = $row->page_parent_id;
            
            // Sub Menu
            
            $this->db->select('*');
            $this->db->where('page_parent_id', $parentitem[$i]['page_id']);
            $subquery = $this->db->get($this->table);
            
            
            foreach($subquery->result() as $row)
            {
                $subitem[$sub]['page_id'] = $row->page_id;
                $subitem[$sub]['page_parent_id'] = $row->page_parent_id;
                $subitem[$sub]['page_menutitle'] = $row->page_menutitle;
                $subparentitem[$sub]['page_parent_id'] = $row->page_parent_id;
                $sub++;
            }
            $i++;    
        }
        $nav;
        $nav = array();
        $nav['parent'] = $parentitem;
        $nav['sub'] = $subitem;
        $nav['subparent'] = $subparentitem;
        return $nav;
        }

 

 

 

Controller :-->

function index(){

$data['nav'] = $this->page->MenuSub();

}

 

 

view :-->

 

<?php
                        foreach($nav['parent'] as $parent)
                            {
                                echo '<li><a name="'.$parent['page_id'].'">'.$parent['page_menutitle'].'</a>';
                                
                                echo '<div>';
                                echo '<ul>';
                                
                                foreach($nav['sub'] as $subitem)
                                {
                                    if($parent['page_id'] == $subitem['page_parent_id']){
                                    echo '<li><a name="'.$subitem['page_parent_id'].'">'.$subitem['page_menutitle'].'</a></li>';
                                    }else{}
                                }
                                echo '</ul>';
                                echo '</div>';
                                
                                    
                                echo '</li>';
                                
                            }
                        ?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.