"id","pid","subject"
1,0,"Food"
2,1,"Beans and Nuts"
3,2,"Beans"
4,2,"Nuts"
5,3,"Black Beans"
6,4,"Pecans"
7,3,"Kidney Beans"
8,7,"Red Kidney Beans"
9,7,"Black Kidney Beans"
10,1,"Dairy"
11,10,"Beverages"
12,11,"Whole Milk"
13,11,"Skim Milk"
14,10,"Cheeses"
15,14,"Cheddar"
16,14,"Stilton"
17,14,"Swiss"
18,14,"Gouda"
19,14,"Muenster"
20,11,"Coffee Milk"
21,0,"Not Food!"
####
$sth->execute;
my @cols = ( );
@cols[0..2] = ( );
$sth->bind_columns( undef, \(@cols) );
while ( $sth->fetch ) {
my $o = Forum::Post->new([ @cols[0..2] ]);
push( @{ $children{ $o->pid } }, $o->id ) and $obj{ $o->id } = $o;
}
####
my %children = (
0 => [1,21],
1 => [2,10],
2 => [3,4],
3 => [5,7],
7 => [8,9],
4 => [6],
10 => [11,14],
11 => [12,13,20],
14 => [15,16,17,18,19]
);
my %obj = (
1 => "Food",
2 => "Beans and Nuts",
3 => "Beans",
4 => "Nuts",
5 => "Black Beans",
6 => "Pecans",
7 => "Kidney Beans",
8 => "Red Kidney Beans",
9 => "Black Kidney Beans",
10 => "Dairy",
11 => "Beverages",
12 => "Whole Milk",
13 => "Skim Milk",
14 => "Cheeses",
15 => "Cheddar",
16 => "Stilton",
17 => "Swiss",
18 => "Gouda",
19 => "Muenster",
20 => "Coffee Milk",
21 => "Not Food!",
);
my @out = ( [1,'Food'],[2,'Beans and Nuts'],[3,'Beans'],[4,'Black Beans'],[4,'Kidney Beans'],[5,'Red Kiney Beans'],[5,'Black Kidney Beans'],[3,'Nuts'],[4,'Pecans'],[2,'Dairy'],[3,'Beverages'],[4,'Whole Milk'],[4,'Skim Milk'], [4,'Coffee Milk'], [3,'Cheeses'], [4,'Cheddar'],[4,'Stilton'],[4,'Swiss'],[4,'Gouda'],[4,'Muenster'],[1,'Not Food!']);
####
my @mystack;
my $i = 1;
tree_children('0');
sub tree_children {
my $r = shift;
# push(@mystack,$obj{$r}) unless $r eq '0';
if ($children{$r}) {
for (@{ $children{$r} }) {
push(@mystack,[$_,$obj{$_}]);
$i++;
tree_children($_);
$i--;
}
}
}