Welcome Guest, Not a member yet? Register   Sign In
Creating a menu from a categories table
#12

[eluser]skunkbad[/eluser]
[quote author="jedd" date="1259915600"]For anyone trying to follow that link - manually change the '#' to '% 23' (no space).

By the way - will your data have sub-menus that contain both leaf items as well as sub-menus themselves, or will it always be a collection of one or the other only?[/quote]

I don't know what these terms are, leaf vs. sub-menus.

I've got a good start on what I'm working on though. Just some procedural stuff:

Code:
<?php
$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;
            }
            else if(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;
                    }
                    else if(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;
                            }
                        }
                    }
                }
            }
        }
    }
}

function buildMenu($menu_array, $is_sub=FALSE)
{
    /*
     * If the supplied array is part of a sub-menu, 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 sub-menu 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";
}

echo buildMenu($menu, FALSE);


Messages In This Thread
Creating a menu from a categories table - by El Forum - 12-03-2009, 10:55 AM
Creating a menu from a categories table - by El Forum - 12-03-2009, 11:53 AM
Creating a menu from a categories table - by El Forum - 12-03-2009, 12:33 PM
Creating a menu from a categories table - by El Forum - 12-03-2009, 02:27 PM
Creating a menu from a categories table - by El Forum - 12-03-2009, 04:45 PM
Creating a menu from a categories table - by El Forum - 12-03-2009, 06:07 PM
Creating a menu from a categories table - by El Forum - 12-03-2009, 06:50 PM
Creating a menu from a categories table - by El Forum - 12-03-2009, 07:06 PM
Creating a menu from a categories table - by El Forum - 12-03-2009, 07:13 PM
Creating a menu from a categories table - by El Forum - 12-03-2009, 08:33 PM
Creating a menu from a categories table - by El Forum - 12-03-2009, 09:49 PM
Creating a menu from a categories table - by El Forum - 12-03-2009, 10:00 PM
Creating a menu from a categories table - by El Forum - 12-03-2009, 11:58 PM
Creating a menu from a categories table - by El Forum - 12-04-2009, 12:10 AM
Creating a menu from a categories table - by El Forum - 12-04-2009, 01:30 AM
Creating a menu from a categories table - by El Forum - 12-04-2009, 02:03 AM
Creating a menu from a categories table - by El Forum - 12-04-2009, 02:14 AM
Creating a menu from a categories table - by El Forum - 12-04-2009, 02:43 AM
Creating a menu from a categories table - by El Forum - 06-16-2010, 04:35 AM
Creating a menu from a categories table - by El Forum - 06-16-2010, 03:02 PM
Creating a menu from a categories table - by El Forum - 06-17-2010, 04:08 AM



Theme © iAndrew 2016 - Forum software by © MyBB