Jump to content

Making directories with recursive function


thara

Recommended Posts

Basically I am having an array something like below, this array has number of categories and sub, sub sub categories.

$categories = array(
    array('id' => 1,  'parent' => 0, 'name' => 'Category'),
    array('id' => 2,  'parent' => 1, 'name' => 'Category A'),
    array('id' => 3,  'parent' => 1, 'name' => 'Category B'),
    array('id' => 4,  'parent' => 1, 'name' => 'Category C'),
    array('id' => 5,  'parent' => 1, 'name' => 'Category D'),
    array('id' => 6,  'parent' => 1, 'name' => 'Category E'),
    array('id' => 7,  'parent' => 2, 'name' => 'Subcategory F'),
    array('id' => 8,  'parent' => 2, 'name' => 'Subcategory G'),
    array('id' => 9,  'parent' => 3, 'name' => 'Subcategory H'),
    array('id' => 10,  'parent' => 4, 'name' => 'Subcategory I'),
    array('id' => 11, 'parent' => 9, 'name' => 'Subcategory J'),
);

Using this array I have created a nested <ul>. This is the recursive function I have used for :

foreach ($categories as $category) {       
   $id     = $category['id'];
   $parent = $category['parent'];
   $name   = $category['name'];

   //echo $name."<br>";
  $cats[$parent][$id] = $name;   
}

function displayList(&$cats, $parent, $current=0, $level=0) {
  switch ($level) {
    case 0:
      $class      = "level_1 has_sub no_active";
      $a_class    = "c1";
      break;
    case 1:
      $class      = "level_2 has_sub no_active";
      $a_class    = "c2";
      break;
    case 2:
      $class      = "level_3 has_sub no_active";
      $a_class    = "c3";
      break;
  }
  if ($parent==0) {
    foreach ($cats[$parent] as $id=>$nm) {
      if (isset($cats[$id])) {     
        displayList($cats, $id, $current);
      }
    }
  }
  else {
    echo "<ul class='$class'>\n";        
    foreach ($cats[$parent] as $id=>$nm) {
      $clear = preg_replace('/[^a-zA-Z0-9\s]/', '', strip_tags(html_entity_decode($nm)));
      $pageName = strtolower(str_replace(' ', '-', $clear));      
      $link = $pageName.'.php';

      echo "<li><a href='$link' class='$a_class $sel'><span>$nm</span></a>\n";

      if (isset($cats[$id])) {
          displayList($cats, $id, $current, $level+1);
      }
      echo '</li>';
    }
    echo "</ul>\n";
  }  
}

Look at that function, I have created a link for each category. Category name itself has used as page name. (Removing by special chars.)

 

My question is, when this function executing I want to create folders for level one categories. Those category names should be used as folder names. 

 

Example from above array:

category_a
category_b
category_c
category_d
category_e

So, every sub category pages should be inside these parent category folders.

 

Can I know is this possible in this function?

 

Thank you.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.