Gossamer Forum
Home : Products : DBMan : Customization :

Browsing Categories

Quote Reply
Browsing Categories
I'm working on designing a classifieds ads system with dbman. I'm interested in adding a new item to the main menu called "Browse Categories" that will make clickable links to all the categories listed under the %db_select_fields in my default.cfg file. I would also like to have it list the total # of ads in each category to the left of the link.

When the user clicks on the link, it will automatically show all the ads in that Category.

Any help on this matter is appreciated! Smile

Thanks,
Frank
Quote Reply
Re: Browsing Categories In reply to
This is another one that I need to make a mod for. Smile

The following will list all of the options in the select field called Category and will count the number of records in each category.

Code:
for ($i = 0; $i <= $#db_cols; $i++) {
if ($db_cols[$i] eq "Category" ) {
$fieldnum = $i; $found = 1;
last;
}
}
if ($found) {
open (DB, "<$db_file_name") or &cgierr("unable to open $db_file_name. Reason: $!");
if ($db_use_flock) { flock(DB, 1); }
LINE: while (<DB> ) {
next if /^#/;
next if /^\s*$/;
$line = $_;
chomp ($line);
@fields = &split_decode ($line);
++$count{$fields[$i]};
}
close DB;

@options = split (/\,/, $db_select_fields{'Category'});

foreach $option (sort @options) {
unless ($count{$option}) {
$count{$option} = '0';
}
print qq|
<a href="$db_script_link_url&Category=$option&view_records=1">$option</a> ($count{$option})<BR>
|;
}
}

Put the above code in the subroutine where you want to print out the list -- probably in sub html_home.


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





Quote Reply
Re: Browsing Categories In reply to
It almost works! Smile The only problem I'm having is that it's not counting the number of ads inside the record. They are all showing up as (0).

Thanks again for all your help!

Frank
Quote Reply
Re: Browsing Categories In reply to
Oops.. I found one more problem with it. It doesn't seem to like spaces in between words in the Category name. Any category that has spaces, indicates- Error: Unknown Action.

For Example: "Computers" will work
"Computer Equipment" will not work.

Thanks!
Frank
Quote Reply
Re: Browsing Categories In reply to
I was afraid you might have spaces in your category names, but I was hoping you wouldn't.

Add the following subroutine to db.cgi:

Code:
sub urlencode {
# --------------------------------------------------------
# Escapes a string to make it suitable for printing as a URL.
#
my($toencode) = shift;
$toencode =~ s/([^a-zA-Z0-9_\-.])/uc sprintf("%%%02x",ord($1))/eg;
return $toencode;
}

Then add the following to the code I gave you earlier, just after

foreach $option (sort @options) {

Code:
$cat_link=&urlencode($option);

Change the link that the script creates to

Code:
<a href="$db_script_link_url&Category=$cat_link&view_records=1">$option</a> ($count{$option})<BR>

As for the counts not showing up, I need you to do a little debugging. After the code I gave you, add

Code:
print $count{'a category that you know is in the database'};

See if anything comes up.

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





Quote Reply
Re: Browsing Categories In reply to
That fixed the problem! Smile

Any ideas on the record counter problem after the Category link?

Frank
Quote Reply
Re: Browsing Categories In reply to
That didn't fix the problem? It may be that you have newline characters in your database. Take a look at your .db file. If you see `` at the beginning of your category field values, you'll need to delete them.


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





Quote Reply
Re: Browsing Categories In reply to
It looks to be correct, although here's a piece of the code:

%db_select_fields = (Category => 'ATVs,Antiques & Jewelry,Apartments for R
ent,Appliances,Auction,Auto Parts,Autos For Sale,Bicycles,Boats & Marine supplie s,Building Supplies,...,Work Wanted', Contact => 'Email,Pager,Phone,Web',
Duration => '1,3,7,14,21,30,60,90'
);

Frank
Quote Reply
Re: Browsing Categories In reply to
Did you look directly at your .db file? Since you had your options on different lines before, any of the records you have added will have incorrect values in them.

You might need to start over with a blank .db file.

Regarding your select fields -- what you have will probably work, but it may be hard to read. Your select field definition should look like

Code:
%db_select_fields = (
Category => 'ATVs,Antiques & Jewelry,Apartments for Rent,...,Work Wanted',
Contact => 'Email,Pager,Phone,Web',
Duration => '1,3,7,14,21,30,60,90'
);

Then again, that may be how you have it and it didn't come out right on the forum.

If you start over with a blank database and you are still having problems, let me know. But I have had this work at least a dozen times before, so I'm not sure what the problem could be.



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





Quote Reply
Re: Browsing Categories In reply to
heheh. Well, now I get this error message whenever I click on a Category, including the ones without spaces:

Search Failed

There were problems with the search. Reason: no matching records.
Please fix any errors and submit the record again.

The debugging string you gave me isn't returning any values at any point inside the subroutine.

Any ideas?

Thanks,
Frank
Quote Reply
Re: Browsing Categories In reply to
Just a shot in the dark, but this is something that occurred to me yesterday. Do you have any spaces between the options in your Category list?

For example, is your list like this:
Code:
Category => 'Autos, Pets for Sale, Pianos'

or is it like
Code:
Category => 'Autos,Pets for Sale,Pianos'

If yours is like the first one, you need to take out the extra spaces. It will cause all sorts of problems, including your "no matching records" and the count not working correctly.

That's the only thing I can think of right now. Maybe I'll come up with something else as the day goes on.

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





Quote Reply
Re: Browsing Categories In reply to
Actually, that might be the case. Here's how it looks in my default.cfg file:

%db_select_fields = (

Category => '
ATV\'s,
Antiques & Jewelry,
Apartments for Rent,
Appliances,
Auction,
Auto Parts,
.
.
.
Work Wanted
',
Contact => 'Email,Pager,Phone,Web',
Duration => '1,3,7,14,21,30,60,90'
);

Frank
Quote Reply
Re: Browsing Categories In reply to
Having them on different lines is even worse than having spaces between them! Smile

The trouble is that there is an unseen newline character after every one of the commas. The script doesn't see a match between

Antiques & Jewelry

and

\nAntiques & Jewelry

You need to put all of your categories on one line, separated only by commas.


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





Quote Reply
Re: Browsing Categories In reply to
I figured out what the problem was. I did a little browsing around on this site, and found a piece of the code that was missing:

I added this code under the line:

@fields = &split_decode ($line);

----------------------------------------
if (!(grep $_ eq $fields[$fieldnum], @selectfields)) {
push (@selectfields, $fields[$fieldnum]);
}
++$count{$fields[$fieldnum]};

It seems to be showing the correct number of records in each category now. Smile

Frank
Quote Reply
Re: Browsing Categories In reply to
Whatever works! Smile

That shouldn't do anything at all for the count, but if it works, then it must be okay!


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





Quote Reply
Re: Browsing Categories In reply to
Doah! I found another problem with the program. I'm using the validation sub, and the view_categories sub also includes the non-validated records in the record count. Is there a way it can be modified to ignore non-validated records?

Much appreciated Smile
Frank
Quote Reply
Re: Browsing Categories In reply to
Yes add these codes to the codes Carol provided you:

Code:
if ($fields[4] eq "Yes") {

before the following line:

Code:
foreach $option (sort @options) {

Replace the [4] with your Validation field.

Then add another } (right bracket) before the following line:

Code:
print qq|

So, the complete codes would look like the following:

Code:
for ($i = 0; $i <= $#db_cols; $i++) {
if ($db_cols[$i] eq "Category" ) {
$fieldnum = $i; $found = 1;
last;
}
}
if ($found) {
open (DB, "<$db_file_name") or &cgierr("unable to open $db_file_name. Reason: $!");
if ($db_use_flock) { flock(DB, 1); }
LINE: while (<DB> ) {
next if /^#/;
next if /^\s*$/;
$line = $_;
chomp ($line);
@fields = &split_decode ($line);
++$count{$fields[$i]};
}
close DB;

@options = split (/\,/, $db_select_fields{'Category'});
if ($fields[4] eq "Yes") {
foreach $option (sort @options) {
unless ($count{$option}) {
$count{$option} = '0';
}
}
print qq|
<a href="$db_script_link_url&Category=$option&view_records=1">$option</a> ($count{$option})<BR>
|;
}
}

------------------
Eliot Lee
Founder and Editor
Anthro TECH, L.L.C
http://www.anthrotech.com/
info@anthrotech.com
==========================
Coconino Community College
http://www.coco.cc.az.us/
Web Technology
Coordinator
elee@coco.cc.az.us

[This message has been edited by Eliot (edited September 11, 1999).]
Quote Reply
Re: Browsing Categories In reply to
I had to make some changes to sub view_categories before it would work correctly, so this is my original code. I tried to apply your change, but it wouldn't list any categories at all.

Let me know if you have any other suggestions! Smile

Thanks!

code:
--------------------------------------------

sub view_categories {
# --------------------------------------------------------
# The database manager home page.

&html_print_headers;
print qq|
<html>
<head>
<title>$html_title: List Categories.</title>
</head>

<body bgcolor="#DDDDDD">
<center>
<table border=1 bgcolor="#FFFFFF" cellpadding=5 cellspacing=3 width=500 align=ce
nter valign=top>
<tr><td colspan=2 bgcolor="navy">
<FONT FACE="MS Sans Serif,Arial,Helvetica" size=1 COLOR="#FFFFFF">
<b>$html_title: Category List</b>
</td></tr>
<tr><td>
<p><center><$font_title><b>Category List</b></font></center><br>
<center><font size=+0.5>Click on the category you wish to view ads for. The num
ber inside the parenthesis indicates how many ads are in that category.</font>
</center>
<HR>
<font face="Verdana,Arial,Helvetica" size="2"><b>
|;
for ($i = 0; $i <= $#db_cols; $i++) {
if ($db_cols[$i] eq "Category" ) {
$fieldnum = $i; $found = 1;
last;
}
}
if ($found) {
open (DB, "<$db_file_name") or &cgierr("unable to open $db_file_name. Reason
: $!");
if ($db_use_flock) { flock(DB, 1); }
LINE: while (<DB> ) {
next if /^#/;
next if /^\s*$/;
$line = $_;
chomp ($line);
@fields = &split_decode ($line);
if (!(grep $_ eq $fields[$fieldnum], @selectfields)) {
push (@selectfields, $fields[$fieldnum]);
}
++$count{$fields[$fieldnum]};
}
close DB;

@options = split (/\,/, $db_select_fields{'Category'});

foreach $option (sort @options) {
$cat_link=&urlencode($option);
unless ($count{$option}) {
$count{$option} = '0';
}
print qq|
<a href="$db_script_link_url&Category=$cat_link&view_records=1">$option</a>
($count{$option})<BR>
|;
}
}

print qq|
</b></font>
</p>
|; &html_footer; print qq|
</td></tr>
</table>
</center>
</body>
</html>
|;
}


[This message has been edited by fharris (edited September 12, 1999).]