Guest User

category sub tree

a guest
Jul 11th, 2013
560
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.66 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Heres your categories array structure, they can be in any order as we will sort them into an hierarchical structure in a moment
  4.  */
  5. $categories = array();
  6. $categories[] = array('id'=>5, 'parent_id' => 4, 'name' => 'Bedroom wear', 'slug' => 'bwear', 'status' => 1);
  7. $categories[] = array('id'=>6, 'parent_id' => 3, 'name' => 'Rolex', 'slug' => 'rolex', 'status' => 1);
  8. $categories[] = array('id'=>1, 'parent_id' => 0, 'name' => 'Men', 'slug' => 'men', 'status' => 1);
  9. $categories[] = array('id'=>2, 'parent_id' => 0, 'name' => 'Women', 'slug' => 'women', 'status' => 1);
  10. $categories[] = array('id'=>3, 'parent_id' => 1, 'name' => 'Watches', 'slug' => 'watches', 'status' => 1);
  11. $categories[] = array('id'=>4, 'parent_id' => 2, 'name' => 'Bras', 'slug' => 'bras', 'status' => 1);
  12. $categories[] = array('id'=>7, 'parent_id' => 2, 'name' => 'Jackets', 'slug' => 'jackets', 'status' => 1);
  13.  
  14.  
  15. /**
  16.  * This function takes the categories and processes them into a nice tree like array
  17.  */
  18. function preprocess_categories($categories) {
  19.  
  20.     // First of all we sort the categories array by parent id!
  21.     // We need the parent to be created before teh children after all?
  22.     $parent_ids = array();
  23.     foreach($categories as $k => $cat) {
  24.         $parent_ids[$k] = $cat['parent_id'];
  25.     }
  26.     array_multisort($parent_ids, SORT_ASC, $categories);
  27.    
  28.     /* note: at this point, the categories are now sorted by the parent_id key */
  29.    
  30.     // $new contains the new categories array which you will pass into the tree function below (nothign fancy here)
  31.     $new = array();
  32.    
  33.     // $refs contain references (aka points) to places in the $new array, this is where the magic happens!
  34.     // without references, it would be difficult to have a completely random mess of categories and process them cleanly
  35.     // but WITH references, we get simple access to children of children of chilren at any point of the loop
  36.     // each key in this array is teh category id, and the value is the "children" array of that category
  37.     // we set up a default reference for top level categories (parent id = 0)
  38.     $refs = array(0=>&$new);
  39.    
  40.     // Loop teh categories (easy peasy)
  41.     foreach($categories as $c) {
  42.        
  43.         // We need the children array so we can make a pointer too it, should any children categories popup
  44.         $c['children'] = array();
  45.        
  46.         // Create the new entry in the $new array, using the pointer from $ref (remember, it may be 10 levels down, not a top level category) hence we need to use the reference/pointer
  47.         $refs[$c['parent_id']][$c['id']] = $c;
  48.        
  49.         // Create a new reference record for this category id
  50.         $refs[$c['id']] = &$refs[$c['parent_id']][$c['id']]['children'];
  51.        
  52.     }
  53.    
  54.     return $new;
  55.    
  56. }
  57.  
  58. /**
  59.  * This function generates our HTML from the categories array we have pre-processed
  60.  */
  61. function tree($categories, $baseurl = '/category/') {
  62.  
  63.      $tree = "<ul>";
  64.      
  65.      foreach($categories as $category) {
  66.      
  67.         $tree .= "<li>";
  68.        
  69.         $tree .= "<a href='".$baseurl.$category['slug']."'>".$category['name']."</a>";
  70.        
  71.         // This is the magci bit, if there are children categories, the function loops back on itself
  72.         // and processes the children as if they were top level categories
  73.         // we append the children to the main tree string rather tha echoing for this reason
  74.         // we also pass the base url PLUS our category slug as the "new base url" so it can build the URL correctly
  75.         if(!empty($category['children'])) {
  76.            
  77.             $tree .= tree($category['children'], $baseurl.$category['slug'].'/');
  78.        
  79.         }
  80.        
  81.         $tree .= "</li>";
  82.        
  83.      
  84.      }
  85.      
  86.      $tree .= "</ul>";
  87.      
  88.      return $tree;
  89.  
  90. }
  91.  
  92. ///echo "<pre>"; print_r(preprocess_categories($categories)); die();
  93. echo tree( preprocess_categories( $categories ) );
  94. ?>
Advertisement
Add Comment
Please, Sign In to add comment