Gossamer Forum
Home : Products : DBMan : Customization :

Displaying by category

Quote Reply
Displaying by category
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)
Quote Reply
Re: Displaying by category In reply to
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
Quote Reply
Re: Displaying by category In reply to
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.
Quote Reply
Re: Displaying by category In reply to
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?
Quote Reply
Re: Displaying by category In reply to
At first glance, your code looks good to me. The question is, does it work? If it works, it's good! Smile

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
Quote Reply
Re: Displaying by category In reply to
Excuse my ignorance but for the life of me I can't figure out how to use the sub &build_select_field_from_db. Could you give me an example my field name is 'Category' that I would like to build from.
Quote Reply
Re: Displaying by category In reply to
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
Quote Reply
Re: Displaying by category In reply to
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
Quote Reply
Re: Displaying by category In reply to
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.
Quote Reply
Re: Displaying by category In reply to
My mistake it isn't a '/' it is an italicized '|'
Quote Reply
Re: Displaying by category In reply to
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"! Smile )





------------------
JPD
Quote Reply
Re: Displaying by category In reply to
JP you are a genius! Thanks a bunch. If you ever need a home mortgage in Canada let me know!

Mike

[This message has been edited by mike1 (edited February 18, 1999).]
Quote Reply
Re: Displaying by category In reply to
Well, there was that little house I saw in Victoria.... Wink

Seriously, just your asking the question gave me the idea of how I could use the same thing. So we both came out ahead.



------------------
JPD