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

[eluser]Federico BaƱa[/eluser]
Try something like this for the cateogry menu:
Code:
// Lets see..
/* here you'll store the categories with its children like this:
"0" = > [
   "{cat_id}" => [
                 "name" => "{cat_name}",
                 "children" => [
                                  "{cat_id}" => [
                                                    ...
                                                ],
                                  "{cat_id}" => [
                                                    ...
                                                ]
                               ]
                 ],
   "{cat_id}" => [
                 "name" => "{cat_name}",
                 "children" => [
                                  "{cat_id}" => [
                                                    ...
                                                ],
                                  "{cat_id}" => [
                                                    ...
                                                ]
                               ]
                 ]
]
*/
//
public $category_tree = array();

// this function will generate that list with the array you have now
// always considering you have the cats sorted by parent_id ASC
function generate_menu()
{
     // define a root category with id 0 and store all the non-children cats inside
     $root = array('children' => array());
     $this->category_tree["0"] = $root;
     // then go trough all the items in the array
     foreach($this->categories as $c)
     {
          // and just add the current category as a child of its parent
          // id the main array is sorted by parent_id asc, at the time of adding children categories we'll already have all the "root" categories stored inside our root array
          $this->category_tree[ $c['parent_id'] ]['children'][] = $c;
     }
     // then remove the first level of the array as its useless from now on
     $this->category_tree = $this->category_tree["0"];
}

hope this helps..
about the other recursives i cant figure out what you want to get there, what is $segs actually? (in the first method)


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