Welcome Guest, Not a member yet? Register   Sign In
More recursive madness
#7

[eluser]skunkbad[/eluser]
I came up with what will be the best solution for my needs. I studied other recursive menus, including my own that are working, and this was the answer:

Code:
<?php

// Function for displaying a list.
// Receives one argument: an array.
function make_list ($child, $parents='')
{
    // Need the main $categories array:
    global $categories;

    // Start a list:
    echo '<ul>';

    // Loop through each subarray:
    foreach ($child as $cat_id => $cat_name)
    {
        // Display the item:
        echo "<li><a href='" . strtolower( "$parents" . "/" . "$cat_name"). "'>$cat_name</a>";

        // Check for children:
        if (isset($categories[$cat_id]))
        {
            // Add parents to URL if applicable
            $new_parents = $parents . '/' . $cat_name;
            
            // Do recursion:
            make_list($categories[$cat_id], $new_parents);
        }

        // Complete the list item:
        echo '</li>';
    }

    // Close the list:
    echo '</ul>';

} // End of make_list() function.


// $categories['parent_id']['category_id'] = 'category_name';
$categories[0][1] = 'Shirts';
$categories[1][2] = 'Mens';
$categories[0][3] = 'Pants';
$categories[3][4] = 'Tight';
$categories[3][5] = 'Loose';
$categories[5][6] = 'With_Belt_Buckles';
$categories[3][7] = 'Baggy';
$categories[5][8] = 'With_Suspenders';
$categories[0][9] = 'Shoes';
$categories[6][10] = 'Green';


// For debugging:
//echo '<pre>' . print_r($categories,1) . '</pre>';

// Send the first array element
// to the make_list() function:
make_list($categories[0]);

?&gt;
Short but sweet!


Messages In This Thread
More recursive madness - by El Forum - 04-20-2010, 06:11 PM
More recursive madness - by El Forum - 04-20-2010, 07:06 PM
More recursive madness - by El Forum - 04-20-2010, 07:09 PM
More recursive madness - by El Forum - 04-20-2010, 08:55 PM
More recursive madness - by El Forum - 04-21-2010, 03:06 AM
More recursive madness - by El Forum - 04-21-2010, 09:11 AM
More recursive madness - by El Forum - 04-21-2010, 02:05 PM



Theme © iAndrew 2016 - Forum software by © MyBB