• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Hello, I'm trying to make a nested structure

#1
[eluser]Unknown[/eluser]
So, based on skunkbad's code, I tried to make a controller to manage a nested structure.

I'm new to Codeigniter, so it may be a easy sollution, still unrevealled for me Sad

Code:
class Menu extends Controller{
    
    function __construct(){
        parent::Controller();
    }
    
    
    function index(){
        
        echo 'No errors!';
    
        $i = array();
        $i[] = array('Examples', 1, 0);
        $i[] = array('Widgets', 2, 1);
        $i[] = array('Things', 3, 0);
        $i[] = array('Shiny', 4, 3);
        $i[] = array('Metal', 5, 4);
        $i[] = array('Silver', 6, 5);
        
        foreach ($i as $a){
            
            //if top level
            if($a[2] == 0){
                //create a top level category
                $menu[$a[0]] = array('category_id' => $a[1]);
            }
            else{
                
                foreach($menu as $b => $c){
                    
                    if($c['category_id'] == $a[2]){
                        
                        $menu[$b]['sub'][$a[0]] = array('category_id' => $a[1]);
                        break;
                    }
                    elseif (array_key_exists('sub', $c)){
                        
                        foreach($c['sub'] as $d => $e){
                            
                            if($e['category_id'] == $a[2]){
                                
                                $menu[$b]['sub'][$d]['sub'][$a[0]] = array('category_id' => $a[1]);
                                break;
                            }
                            elseif(array_key_exists('sub', $e)){
                                
                                foreach($e['sub'] as $f => $g){
                                    
                                    if($g['category_id'] == $a[2]){
                                        
                                        $menu[$b]['sub'][$d]['sub'][$f]['sub'][$a[0]] = array('category_id' => $a[1]);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        
        $this->buildMenu($menu, FALSE);
    }
    
    
    function buildMenu($menu_array, $is_sub = FALSE){
        
        /**
        * If the supplied array is part of a submenu, add the sub-menu class instead
        * of the menu ID for CSS styling
        */
        
        $attr = (!$is_sub) ? ' id="menu"' : 'class="submenu"';
        $menu = "<ul$attr>\n"; // open the menu container
        
        /**
        * Loop through the array to extract element values
        */
        
        foreach($menu_array as $id => $properties){    
            
            /**
            * Because each page element is another array, we need to loop again
            * This time we save individual array elements as variables, using the array
            * key as the variable name.
            */
            
            foreach($properties as $key => $val){
                
                /**
                * If the array element contains another array, call the buildMenu()
                * function recursively to build the submenu and store it in $sub
                */
                
                if(is_array($val)){
                    
                    $sub = buildMenu($val, TRUE);
                }
                
                /**
                * Otherwise, set $sub to NULL and store the element's value in a variable
                */
                
                else{
                    
                    $sub = NULL;
                    $$key = $val;
                }
            }
            
            /**
            * If no array element had the key 'url', set the $url variable equal to the containing
            * element's ID
            */
            
            if(!isset($url)){
                
                $url = $id;
            }
            
            /**
            * Use the created variables to output HTML
            */
            
            $menu .= '<li><a href = "$url">$url</a>$sub</li>\n';
            
            /**
            * Destroy the variables to ensure they're reset on each iteration
            */
            
            unset($url, $display, $sub);
    
        }
        
        return $menu . "</ul>\n";
    }
}

When I use this, I get 2 php errors:

Quote:A PHP Error was encountered
Severity: Warning
Message: Missing argument 1 for buildmenu()
Filename: controllers/menu.php
Line Number: 68

A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: controllers/menu.php
Line Number: 82

Can anyone know what is happening, because I use the function buildMenu() from inside controller's index() function, and $menu is a valid parameter which I think I passed it correctly.

#2
[eluser]danmontgomery[/eluser]
Code:
if($a[2] == 0){
                //create a top level category
                $menu[$a[0]] = array('category_id' => $a[1]);
            }
            else{
                
                foreach($menu as $b => $c){

in your foreach() here menu has never been declared, so buildMenu is being passed a variable that doesn't exist.

#3
[eluser]Unknown[/eluser]
I defined $menu = array(); just above $i. Still same errors.

#4
[eluser]danmontgomery[/eluser]
Code:
$sub = buildMenu($val, TRUE);

should be

Code:
$sub = $this->buildMenu($val, TRUE);


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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