Thursday, 8 August 2013

populate parent child select box in perl

populate parent child select box in perl

I want to populate my select drop down with nested parent and child
relationship which comes through database table.i.e. if firstly I create a
child , then create child of child that comes like: my table as follows:
forumid uid parentid is_category title
136720 106 -1 0 Android
136666 106 -1 0 Main Forum
136665 106 -1 0 test
136719 106 -1 1 Nokia C2-01
136954 106 136666 0 add child
136994 106 136665 0 sub form under forum
136997 106 136720 0 under category
137004 106 136954 0 child of child
I am doing like this but not getting expected result
<select name="parentid" id="parentid" class='dropDown'>
~;
my ($label_open, $to_forum, @parentId);
my $forums = $DB{mb} -> selectall_arrayref("SELECT
is_category,forumid,parentid,title,displayorder,forumpw,forum_link,invisible,locked,threadcount
FROM forums WHERE uid='$global{uid}' ORDER BY displayorder",{ Slice =>
{} });
if ($settings{reqreg}) {
# get all the forumids and subforumids in an array
foreach my $forum ( @{$forums}){
push(@parentId, {forumid=>$forum->{forumid},
parentid=>$forum->{parentid},title=>$forum->{title}});
}
}
foreach my $forum (@{$forums}){
if ($label_open) {
print qq~</optgroup>~;
$label_open = 0;
}
if ($forum->{is_category}) {
print qq~&nbsp;&nbsp;<optgroup label="$forum->{title}"
value="$forum->{forumid}">~;
$label_open = 1;
}else {
print qq~<option
value="$forum->{forumid}">$forum->{title}</option>~;
foreach my $parentid(@parentId){
if($forum->{forumid} == $parentid->{parentid}){ print
STDERR "\nwewe7777777\n";
print qq~&nbsp;&nbsp;<optgroup
label="$parentid->{title}"
value="$parentid->{forumid}">~;
$label_open = 1;
}
}
}
}
if ($label_open) {
print qq~</optgroup>~;
$label_open = 0;
}
print qq~</select>
the output should be:
Main Forum -add child --child of child test -sub form under test forum
Nokia C2-01 -Android --under category

No comments:

Post a Comment