Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Unlimited groups/categories

by friedo (Prior)
on Jun 01, 2008 at 01:48 UTC ( #689484=note: print w/replies, xml ) Need Help??


in reply to Unlimited groups/categories

For trivial tree-processing problems, a recursive solution is generally easiest. This might not be the best way if your tree is truly gigantic, but it should work in most cases. This is untested but should get you on the right track:

my $dbh = ... # set up database sub display_groups { my ( $parent, $level ) = @_; # get all children of this parent my $sth = $dbh->prepare("SELECT id, gname FROM groups WHERE subgroup=? ORDER BY gname"); $sth->execute( $parent ); # display each child and its descendants while( my $row = $sth->fetchrow_hashref ) { print " " x $level; # indent print $row->{gname}, "\n"; display_groups( $row->{id}, $level + 1 ); } } # start with children of group 0 display_groups( 0, 0 );

Update: Added ORDER BY clause.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://689484]
help
Chatterbox?
[Corion]: Whee! Germanys Grand Central Airport (for Berlin, re-scheduled for the thrid or fourth time, to open now in 2018) has just fired their technical lead
[ambrus]: Corion: Is their budget estimate four times the initial estimate yet? Was the construction late by six months already three months after they started?
[marto]: I'll believe it when I see it. That's been due for many years now :P
[marto]: oddly the last time I was there I saw a zeppelin in flight for the first time
[Corion]: ambrus: I don't know how far the budget estimate has been overrun yet

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (12)
As of 2017-02-23 11:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?






    Results (345 votes). Check out past polls.