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
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