How would I go about setting up dbman to allow users to add items to categories, possibly add categories and then view items grouped by category? Any suggestions to get me started would be greatly appreciated. (What I want to set up is a DB of 'Notes & Quotes' for our public speaking club)
Feb 17, 1999, 8:42 PM
Veteran / Moderator (8669 posts)
Feb 17, 1999, 8:42 PM
Post #2 of 13
Views: 778
I'm not really clear on what you want, but I'll take a stab at it.
To add items to categories, one would just add an item and set the category. (Unless I'm really misunderstanding you!)
To add categories, you would have to set up the form to be a text field, not a drop-down list. You could have two forms -- one to search and one to add and modify. The searching one could use the &build_select_field_from_db function to get a drop-down list of all the fields that had been added, while the add/modify form would have a text field for inputting category.
To group by category, you'd have to either search for items that were in that category, or sort by the category field. I don't think you can do a "group by" function unless you're using SQL.
But, again, I might not be understanding what you mean.
------------------
JPD
To add items to categories, one would just add an item and set the category. (Unless I'm really misunderstanding you!)
To add categories, you would have to set up the form to be a text field, not a drop-down list. You could have two forms -- one to search and one to add and modify. The searching one could use the &build_select_field_from_db function to get a drop-down list of all the fields that had been added, while the add/modify form would have a text field for inputting category.
To group by category, you'd have to either search for items that were in that category, or sort by the category field. I don't think you can do a "group by" function unless you're using SQL.
But, again, I might not be understanding what you mean.
------------------
JPD
This is the idea that I would like to accomplish. Perhaps you can help me with the proper syntax to do it.
sub html_view_success {
# --------------------------------------------------------
# This page displays the results of a successful search.
# You can use the following variables when displaying your
# results:
#
# $numhits - the number of hits in this batch of results.
# $maxhits - the max number of hits displayed.
# $db_total_hits - the total number of hits.
# $db_next_hits - html for displaying the next set of results.
#
my (@hits) = @_;
my ($numhits) = ($#hits+1) / ($#db_cols+1);
my ($maxhits); $in{'mh'} ? ($maxhits = $in{'mh'}) : ($maxhits = $db_max_hits);
&html_print_headers;
print qq|
<html>
<head>
<title>$html_title: Search Results.</title>
</head>
<body bgcolor="#DDDDDD">
<blockquote><center>
<FONT FACE="MS Sans Serif, arial,helvetica" size=1 COLOR="#000000">
<b>$html_title: Search Results</b>
</font></td></tr>
<tr><td>
<p><$font>
Your search returned <b>$db_total_hits</b> matches.</font>
|;
if ($db_next_hits) {
print "<br><$font>Pages: $db_next_hits</font>";
}
print"</td></tr></table>";
#Repeat this code on change in category
$tempcategory=$rec{'category'};
print"<br>Category: $rec{'category'}"
# Record would always have to be sorted by category.
# Go through each hit and convert the array to hash and send to
# html_record for printing.
for (0 .. $numhits - 1) {
print "<center>";
&html_record (&array_to_hash($_, @hits));#html_record would only contain the 'quote' field and not reprint the category field.
if $rec{'category'} ne $tempcategory {
on change in category go back and print new category title and reset $tempcategory.}#I don't know the proper syntax to do this.
print "</center>";
}
if ($db_next_hits) {
print qq~ <TABLE BORDER=1 BGCOLOR="#DDDDDD" CELLSPACING=0 CELLPADDING=3 WIDTH=500>
<TR>
<TD WIDTH=500 BGCOLOR="#FFFFcc">
<CENTER><A HREF="http"><FONT COLOR="#000000"></A>Pages: $db_next_hits</FONT></CENTER>
</TD>
</TR>
</TABLE>~;
}
print qq|
<table border=1 bgcolor="#ffffff" cellpadding=5 cellspacing=3 width=500 valign=top>
<tr><td>|; &html_footer; print qq|</td></tr>
</table></center>
</blockquote>
</body>
</html>
|;
}
I understand how to have a field category and sort by category I guess what I am looking for on the add side of things is if a user wants to add a 'note or quote' related to say 'perseverance' and another user had already added that category they should be able to select from a list of existing categories or add a new one if applicable. Could this be done by giving them a drop down and a text field with an option button beside each one so if they do not find the category they want in the drop down they can select the text field and add one? Of course then the drop down would have to refresh itself with the added categories.
sub html_view_success {
# --------------------------------------------------------
# This page displays the results of a successful search.
# You can use the following variables when displaying your
# results:
#
# $numhits - the number of hits in this batch of results.
# $maxhits - the max number of hits displayed.
# $db_total_hits - the total number of hits.
# $db_next_hits - html for displaying the next set of results.
#
my (@hits) = @_;
my ($numhits) = ($#hits+1) / ($#db_cols+1);
my ($maxhits); $in{'mh'} ? ($maxhits = $in{'mh'}) : ($maxhits = $db_max_hits);
&html_print_headers;
print qq|
<html>
<head>
<title>$html_title: Search Results.</title>
</head>
<body bgcolor="#DDDDDD">
<blockquote><center>
<FONT FACE="MS Sans Serif, arial,helvetica" size=1 COLOR="#000000">
<b>$html_title: Search Results</b>
</font></td></tr>
<tr><td>
<p><$font>
Your search returned <b>$db_total_hits</b> matches.</font>
|;
if ($db_next_hits) {
print "<br><$font>Pages: $db_next_hits</font>";
}
print"</td></tr></table>";
#Repeat this code on change in category
$tempcategory=$rec{'category'};
print"<br>Category: $rec{'category'}"
# Record would always have to be sorted by category.
# Go through each hit and convert the array to hash and send to
# html_record for printing.
for (0 .. $numhits - 1) {
print "<center>";
&html_record (&array_to_hash($_, @hits));#html_record would only contain the 'quote' field and not reprint the category field.
if $rec{'category'} ne $tempcategory {
on change in category go back and print new category title and reset $tempcategory.}#I don't know the proper syntax to do this.
print "</center>";
}
if ($db_next_hits) {
print qq~ <TABLE BORDER=1 BGCOLOR="#DDDDDD" CELLSPACING=0 CELLPADDING=3 WIDTH=500>
<TR>
<TD WIDTH=500 BGCOLOR="#FFFFcc">
<CENTER><A HREF="http"><FONT COLOR="#000000"></A>Pages: $db_next_hits</FONT></CENTER>
</TD>
</TR>
</TABLE>~;
}
print qq|
<table border=1 bgcolor="#ffffff" cellpadding=5 cellspacing=3 width=500 valign=top>
<tr><td>|; &html_footer; print qq|</td></tr>
</table></center>
</blockquote>
</body>
</html>
|;
}
I understand how to have a field category and sort by category I guess what I am looking for on the add side of things is if a user wants to add a 'note or quote' related to say 'perseverance' and another user had already added that category they should be able to select from a list of existing categories or add a new one if applicable. Could this be done by giving them a drop down and a text field with an option button beside each one so if they do not find the category they want in the drop down they can select the text field and add one? Of course then the drop down would have to refresh itself with the added categories.
JP you might even be proud of me. I got it. (Although I am open to suggestions if you see flaws in my code)
First define:
sub tmp_category{
my (%rec) = @_;
($db_auto_generate and print &build_html_record(%rec) and return);
$tmp_category=$rec{'Category'};
print"<br><em>$tmp_category</em>";#whatever format you want the category title to look like
}
Call this sub from view_success:
&tmp_category (&array_to_hash($_, @hits));
just before this line --for (0 .. $numhits - 1) {
In the html_record insert the following:
if ($rec{'Category'} ne $tmp_category ){
&tmp_category;
}#if category has changed print new category header and redefine $tmp_category
prior to the format of your record display
What do you think?
First define:
sub tmp_category{
my (%rec) = @_;
($db_auto_generate and print &build_html_record(%rec) and return);
$tmp_category=$rec{'Category'};
print"<br><em>$tmp_category</em>";#whatever format you want the category title to look like
}
Call this sub from view_success:
&tmp_category (&array_to_hash($_, @hits));
just before this line --for (0 .. $numhits - 1) {
In the html_record insert the following:
if ($rec{'Category'} ne $tmp_category ){
&tmp_category;
}#if category has changed print new category header and redefine $tmp_category
prior to the format of your record display
What do you think?
Feb 18, 1999, 10:13 AM
Veteran / Moderator (8669 posts)
Feb 18, 1999, 10:13 AM
Post #5 of 13
Views: 777
At first glance, your code looks good to me. The question is, does it work? If it works, it's good!
Regarding adding new categories --
It never hurts to try something new, as long as you save backups and test each thing as you add it to be sure you know where the problem is.
You could try using &build_select_field_from_db and a text field, both with the field name of "Category." As long as the user doesn't select an entry from the select field, whatever is in the text field should be used by DBMan. You wouldn't need a radio button, though.
If you use &build_select_field_from_db, you don't have to worry about refreshing the list. The next time it's displayed it'll give you want you want.
Please let us know how it works. This is something a lot of other people might want to use.
------------------
JPD
Regarding adding new categories --
It never hurts to try something new, as long as you save backups and test each thing as you add it to be sure you know where the problem is.
You could try using &build_select_field_from_db and a text field, both with the field name of "Category." As long as the user doesn't select an entry from the select field, whatever is in the text field should be used by DBMan. You wouldn't need a radio button, though.
If you use &build_select_field_from_db, you don't have to worry about refreshing the list. The next time it's displayed it'll give you want you want.
Please let us know how it works. This is something a lot of other people might want to use.
------------------
JPD
Feb 18, 1999, 11:24 AM
Veteran / Moderator (8669 posts)
Feb 18, 1999, 11:24 AM
Post #7 of 13
Views: 778
Sure. I don't think it's documented in the readme file.
Just use it like you would &build_select_field --
print &build_select_field_from_db("Category",$rec{'Category'});
Make sure you have version 2.04 for this!
If you're not sure, open up db.cgi and do a search for build_select_field_from_db.
------------------
JPD
Just use it like you would &build_select_field --
print &build_select_field_from_db("Category",$rec{'Category'});
Make sure you have version 2.04 for this!
If you're not sure, open up db.cgi and do a search for build_select_field_from_db.
------------------
JPD
Feb 18, 1999, 12:02 PM
User (305 posts)
Feb 18, 1999, 12:02 PM
Post #8 of 13
Views: 777
Hi Mike...
I am interested in doing the same kind of thing. Do you have a URL where I can see what you've done?
Thanks.
------------------
Jason
Extreme mtb
http://extreme.nas.net
I am interested in doing the same kind of thing. Do you have a URL where I can see what you've done?
Thanks.
------------------
Jason
Extreme mtb
http://extreme.nas.net
Feb 18, 1999, 12:05 PM
User (91 posts)
Feb 18, 1999, 12:05 PM
Post #9 of 13
Views: 777
JP, Thanks again! I have got it working except one minor glitch. When I select a category from the drop down it adds a trailing '/' any idea where I can fix this. P.S. I also need to address some security issues as my cgi-bin is fully accessable via the net. Is there a way I can secure it. I tried .htaccess but then you can't even run a script from it without authentication.
Feb 18, 1999, 12:57 PM
Veteran / Moderator (8669 posts)
Feb 18, 1999, 12:57 PM
Post #11 of 13
Views: 777
The trailing | is something I was afraid would happen. It's there so you can make multiple selections from a select list.
Hmmmm. Let me think for a sec.
I have a "maybe" fix. Go into db.cgi and search for the parse_form subroutine. After
if ($value eq "---") { next PAIR; }
add
unless ($value) { next PAIR; }
That should do it. (The operative word being "should"!
)
------------------
JPD
Hmmmm. Let me think for a sec.
I have a "maybe" fix. Go into db.cgi and search for the parse_form subroutine. After
if ($value eq "---") { next PAIR; }
add
unless ($value) { next PAIR; }
That should do it. (The operative word being "should"!
------------------
JPD

