Gossamer Forum
Home : Products : DBMan : Customization :

Pl help with a category select mod

Quote Reply
Pl help with a category select mod
I want to have users select a category before adding a record and carry his selection over to the add_form. Thanks to JPDeni, this was implemented very well. Since you can not put your category list on a static page outside dbman hoping it will send info like $uid to the add_form, I set up a new subroutine called sub select_category to do this.
1) I added a line in db.cgi
elsif ($in{'select_category'}) { if ($per_add) { &select_category; } else { &html_unauth; } } #select a category before add a record

2) In html.pl, I added a new subroutine named sub select_category which can be called by including &select_category=1 in url.In this subroutine, I just list my categories and subcategories with link to the actual add-form and some other varibles. They look like this:
print qq| Category1
<ul>
<li><a href="$db_script_link_url&add_form=1&Category=Category1&Subcategory=Sub1>Sub1</a></li>
<li><a href="$db_script_link_url&add_form=1&Category=Category1&Subcategory=Sub2>Sub2</a></li>
</ul>
Category2
<ul>
<li><a href="$db_script_link_url&add_form=1&Category=Category2&Subcategory=Sub1>Sub1</a></li>
<li><a href="$db_script_link_url&add_form=1&Category=Category2&Subcategory=Sub1>Sub1</a></li>
</ul>|;

3) Since I have many categories and each has many subcategories under it, it will very time-consuming to manually edit the category list, so I set up an other subroutine to build the hyperlinked category list. First, I add a hash in my db.cfg:
%db_select_category = (
'Category1' => 'Subcategory1,Subcategory2',
'Category2' => 'Subcategory1,Subcategory2',
);

4) I add a subroutine in db.cfg:

sub build_category_link {
# --------------------------------------------------------
# Builds a category selction page based on information found
# in the database definition.
my ($categoryname, $subcategoryname) = @_;
my ($categoryvalue,@subcategoryvalue, $output);
foreach $key (sort (keys %db_select_category)){
$categoryvalue=$key;
@subcategoryvalue=split(/\,/,$db_select_category{$key});
$output =qq|$categoryvalue<ul>;
for (i=0;i<=$#subcategory;i++){
$output .=<li><a href=$db_script_link_url&add_form=1&$categoryname=$categoryvalue&$subcategoryname=$subcategoryvalue>$subcategoryvalue</a></li>;
}
$output .=</ul>|;
}
return $output;
}

5) In html.pl sub select_category, I add this line

print &build_category_link("Category","Subcategory");
# "Category","Subcategory" are field name of my db.

When I call my subroutines by typing the url http://xxx/.. &select_category=1, everything is fine except some display problem, such as the "for (i=0...)" statement is displayed. Apparently, something must be wrong with the "$output .= " lines. Since I am quite new to Perl, I hope some of you would check my codes in sub build_category_link. I am sure there must be many errors in it.

Thank you in advance.

Long

Quote Reply
Re: Pl help with a category select mod In reply to
Not too bad. Just a couple of things.

Code:

sub build_category_link {
# --------------------------------------------------------
# Builds a category selction page based on information found
# in the database definition.
my ($categoryname, $subcategoryname) = @_;
my ($categoryvalue,@subcategoryvalue, $output);
foreach $key (sort (keys %db_select_category)){
$categoryvalue=$key;
@subcategoryvalue=split(/\,/,$db_select_category{$key});
$output =qq|$categoryvalue<ul>|;
for ($i=0;$i<=$#subcategory;$i++){
$output .=qq|<li><a
href=$db_script_link_url&add_form=1&$categoryname=$categoryvalue
&$subcategoryname=$subcategoryvalue>$subcategoryvalue</a></li>|;
}
$output .=qq|</ul>|;
}
return $output;
}
JPD
http://www.jpdeni.com/dbman/
Quote Reply
Re: Pl help with a category select mod In reply to
Thanks, JPD!

I tried the correct code, one problem is it only displays one category. For example, I have defined the hash %db_select_category in .cfg as follows:

'Cat1' =>'sub1,sub2,sub3',
'Cat2' =>'sub1,sub2,sub3',
'Cat3' =>'sub1,sub2,sub3',

It only displays Cat3 and its subcategories with correct links. According to the code, it should sort all the 3 key/value pairs, how can it just stop at the first one?

I made some minor corrections to the subroutine.

sub build_category_link {
# --------------------------------------------------------
# Builds a category selction page based on information found
# in the database definition.
my ($categoryname, $subcategoryname) = @_;
my ($categoryvalue,@subcategoryvalue, $output);
foreach $key (sort (keys %db_select_category)){
$categoryvalue=$key;
@subcategoryvalue=split(/\,/,$db_select_category{$key});
$output =qq|$categoryvalue<ul>|;
for ($i=0;$i<=$#<red>subcategoryvalue</red>;$i++){
$output .=qq|<li><a href=$db_script_link_url&add_form=1&$categoryname=$categoryvalue&$subcategoryname=$subcategoryvalue[<red>[$i]</red>$subcategoryvalue<red>[$i]</red></a></li>|;
}
$output .=qq|</ul>|;
}
return $output;
}

Quote Reply
Re: Pl help with a category select mod In reply to
Actually, you're only getting the last one.

Make the change in red below:

$output .=qq|$categoryvalue<ul>|;

That's right. All you need to add is a .! Smile

(Sorry I missed that before.)

JPD
http://www.jpdeni.com/dbman/
Quote Reply
Re: Pl help with a category select mod In reply to
It works great.

Thanks a lot, JPD!

Long