views:

327

answers:

1

Hi, I am pretty new to CodeIgniter and PHP in general and have two questions that are somewhat related.

I have a nested category tree with unlimited depth that produces links like myapp.com/language and myapp.com/spanish but I would like to have something like myapp.com/language/spanish.

For reference my model returns an array of arrays like:

[Languages] => Array ( [category_id] => 4 [sub] => Array ( [Japanese] => Array ( [category_id] => 5 ) [Spanish] => Array ( [category_id] => 6 )...

My helper function to create the menu:

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";

This leads me to my next problem. How do I get these links to call a dynamic function that can return a list of all posts in that category. I am thinking that if I can get the routes working how I want them above that it will be as easy as having a function like get_cat_posts($cat_id). Am I correct in this assumption? Will having more than one sub-level be a problem?

Sorry for the long post. Thanks for any help.

A: 

Well I have given up on trying to make this work. It seems untenable to try using this current function to produces the results that I desire so I have moved on to just using an adjacency list and caching the results. Meh.

Mike T.