All pastes #112755 Raw Edit

Stuff

public text v1 · immutable
·published 2006-08-03 05:05 UTC
rendered paste body
$arrCategoryTree = array();

while ($result_row = mysql_fetch_row($objRS)) {    
	$arrCategoryTree[] = array (
						'category_id' => $result_row[0],
						'parent_category_id' => $result_row[1],
						'category_name' => $result_row[2]
						);
}

//var_dump ($arrCategoryTree);

function tree($id_parent = 0, $indent = 0) 
{ 
global $arrCategoryTree, $ret_tree; 
$tt = array(); 
$x = 0; 
$loop = 0; 

foreach($arrCategoryTree as $n) 
{          
    if( $n['parent_category_id'] == $id_parent) 
    { 
        $tt[$x++] = $loop;   
    }   
    $loop++; 
} 
/*-- if there are some parent ids --*/ 
if( $x != 0){              
    foreach($tt as $d) 
    { 
        $tmp = array(); 
         
        foreach($arrCategoryTree[$d] as $key => $value) 
        {                 
            $tmp[$key] = $value;  
        } 
        $tmp['indent'] = $indent; 

        $ret_tree[] = $tmp; 
         
        tree($tmp['category_id'], $indent+1); 
    } 
} 
else 
{ 
    return; 
} 
} 

$ret_tree = array(); 

tree(0,0); 

 				
foreach($ret_tree as $cat)
{ 
    $indent = ''; 
    
    $indent .= str_repeat ("\t", $cat['indent']); 
   
    if ($intPreviousIndent <= $cat['indent'])
    	echo ("</category>\n");
    	
    	 
    echo ($indent .  $cat['category_name']);
    
    //target structure
    /*
    <category name="rootfolder" id="0">
		<category name="folder1" id="1"/>
		<category name="folder2" id="2">
			< category name="subfolder2.1" id="3"/>
		</category >
	</category >


	*/

     //current structure
     /*
array(13) {
  [0]=>
  array(4) {
    ["category_id"]=>
    string(1) "1"
    ["parent_category_id"]=>
    string(1) "0"
    ["category_name"]=>
    string(8) "My Media"
    ["indent"]=>
    int(0)
  }
  [1]=>
  array(4) {
    ["category_id"]=>
    string(1) "2"
    ["parent_category_id"]=>
    string(1) "1"
    ["category_name"]=>
    string(6) "School"
    ["indent"]=>
    int(1)
  }
  [2]=>
  array(4) {
    ["category_id"]=>
    string(1) "3"
    ["parent_category_id"]=>
    string(1) "2"
    ["category_name"]=>
    string(13) "English Class"
    ["indent"]=>
    int(2)
  }
  [3]=>
  array(4) {
    ["category_id"]=>
    string(1) "4"
    ["parent_category_id"]=>
    string(1) "2"
    ["category_name"]=>
    string(13) "Spanish Class"
    ["indent"]=>
    int(2)
  }
  [4]=>
  array(4) {
    ["category_id"]=>
    string(1) "5"
    ["parent_category_id"]=>
    string(1) "1"
    ["category_name"]=>
    string(13) "Family Photos"
    ["indent"]=>
    int(1)
  }
  [5]=>
  array(4) {
    ["category_id"]=>
    string(1) "6"
    ["parent_category_id"]=>
    string(1) "1"
    ["category_name"]=>
    string(13) "Sports Photos"
    ["indent"]=>
    int(1)
  }
  [6]=>
  array(4) {
    ["category_id"]=>
    string(1) "7"
    ["parent_category_id"]=>
    string(1) "6"
    ["category_name"]=>
    string(6) "Soccer"
    ["indent"]=>
    int(2)
  }
  [7]=>
  array(4) {
    ["category_id"]=>
    string(1) "8"
    ["parent_category_id"]=>
    string(1) "6"
    ["category_name"]=>
    string(10) "Basketball"
    ["indent"]=>
    int(2)
  }
  [8]=>
  array(4) {
    ["category_id"]=>
    string(1) "9"
    ["parent_category_id"]=>
    string(1) "6"
    ["category_name"]=>
    string(8) "Football"
    ["indent"]=>
    int(2)
  }
  [9]=>
  array(4) {
    ["category_id"]=>
    string(2) "10"
    ["parent_category_id"]=>
    string(1) "1"
    ["category_name"]=>
    string(11) "My 2nd Main"
    ["indent"]=>
    int(1)
  }
  [10]=>
  array(4) {
    ["category_id"]=>
    string(2) "11"
    ["parent_category_id"]=>
    string(2) "10"
    ["category_name"]=>
    string(14) "2nd Main Sub-1"
    ["indent"]=>
    int(2)
  }
  [11]=>
  array(4) {
    ["category_id"]=>
    string(2) "12"
    ["parent_category_id"]=>
    string(2) "11"
    ["category_name"]=>
    string(14) "2nd Main Sub-2"
    ["indent"]=>
    int(3)
  }
  [12]=>
  array(4) {
    ["category_id"]=>
    string(2) "13"
    ["parent_category_id"]=>
    string(2) "10"
    ["category_name"]=>
    string(16) "2nd Main Sub-1-1"
    ["indent"]=>
    int(2)
  }
}*/




    
    $intPreviousIndent = $cat['indent'];
}