Gossamer Forum
Home : Products : DBMan : Customization :

Record Count

Quote Reply
Record Count
I have a pulldown menu with approx 10 cities to select from. What I would like to do is for dbman to count the number of records in each city and show show it in the pull down menu.

Any help would be greatly appreciated.

Fred
Quote Reply
Re: Record Count In reply to
You want to show the number of entries for each city in the select list? Ooooh. That's a toughie.

Getting the number of entries for each possible option is not a problem. That has been discussed a whole lot in
http://www.gossamer-threads.com/scripts/forum/resources/Forum12/HTML/000235.html

Creating the select field to include the numbers is much more of a problem. I'm gonna have to ponder this one for a bit.


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





Quote Reply
Re: Record Count In reply to
Okay. I think I got it.

In db.cgi, change

sub build_select_field_from_db

to the following:

Code:
sub build_select_field_from_db {
# --------------------------------------------------------
# Builds a SELECT field from the database.

my ($column, $value, $name) = @_;
my (@fields, $field, @selectfields, @lines, $line, $ouptut);
my ($fieldnum, $found, $i) = 0;

$name | | ($name = $column);

for ($i = 0; $i <= $#db_cols; $i++) {
if ($column eq $db_cols[$i]) {
$fieldnum = $i; $found = 1;
last;
}
}
if (!$found) {
return "error building select field: no fields specified!";
}

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[$fieldnum]};
if (!(grep $_ eq $fields[$fieldnum], @selectfields)) {
push (@selectfields, $fields[$fieldnum]);
}
}
close DB;

$output = qq|<SELECT NAME="$name"><OPTION>---|;
foreach $field (sort @selectfields) {
($field eq $value) ?
($output .= qq|<OPTION SELECTED VALUE="$field">$field ($count{$field})|) :
($output .= qq|<OPTION VALUE="$field">$field ($count{$field})|);
}
$output .= "</SELECT>";

return $output;
}

Be sure to delete the space between the two | characters above that the forum software inserts.

In html_record_form (or, in your case, since we've talked on the phone, html_search_form), where you want to print out your select field, use

|;
print &build_select_field_from_db("City",$rec{'City'}");
print qq|

and then go on with printing your form.

Change City to the exact name of your field.

This will make a select field that has only cities that are currently in the database.


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





Quote Reply
Re: Record Count In reply to
Where is the script? I'll take a look at it.


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





Quote Reply
Re: Record Count In reply to
The first part works in the dg.cgi but when I place the:

|;
print &build_select_field_from_db("City",$rec{'City'}");
print qq|

in the .pl file. I get back "too many errors"

Fred
Quote Reply
Re: Record Count In reply to
Thats working great. Thanks...

However, its not going to work. I have to also show those cities with a (0) zero record in the db. Just to let people know that there aren't any in their particular city.

Thanks
Fred
Quote Reply
Re: Record Count In reply to
Okay. Add this subroutine to db.cgi

Code:
sub build_counted_select_field {
# --------------------------------------------------------
# Builds a SELECT field based on information found
# in the database definition. Parameters are the column to build
# and a default value (optional).

my ($column, $value) = @_;
my (@fields, $ouptut);

open (DB, "<$db_file_name") or
&cgierr(" unable to open db file: $db_file_name.\nReason: $!");
if ($db_use_flock) { flock(DB, 1); }
@lines = <DB>;
close DB;
for ($i = 0; $i <= $#db_cols; $i++) {
if ($column eq $db_cols[$i]) {
$fieldnum = $i; $found = 1;
last;
}
}
if (!$found) {
return "error building select field: no fields specified!";
}
foreach $line (@lines) {
@data = &split_decode($line);
++$count{$data[$fieldnum]};
}

@fields = split (/\,/, $db_select_fields{$column});
if ($#fields == -1) {
$output = "error building select field: no select fields specified in config for field '$column'!";
}
else {
$output = qq|<SELECT NAME="$column"><OPTION>---|;
foreach $field (@fields) {
unless ($count{$field}) {
$count{$field} = "0";
}
$field eq $value ?
($output .= qq|<OPTION SELECTED VALUE="$field">$field ( $count{$field} )\n|) :
($output .= qq|<OPTION VALUE="$field">$field ( $count{$field} )|);
}
$output .= "</SELECT>";
}
return $output;
}

Then use

|;
print &build_counted_select_field("City","$rec{'City'}");
print qq|

instead of the &build_select_field_from_db

One thing you'll have to be sure of is that your city entries are the same as the ones in your select field. I noticed on your site (which looks very nice! Smile ) that there were entries for both "Kent" and "KENT." They all have to be the same as the select field is defined in the .cfg file.


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





Quote Reply
Re: Record Count In reply to
Thanks... how do I place that in regards to the city's listed?

<SELECT NAME="City" SIZE=15 MULTIPLE>
<OPTION value = "Algona" Selected>Algona
<OPTION value = "Auburn" >Auburn
<OPTION value = "Baring" >Baring
<OPTION value = "Bellevue" >Bellevue
<OPTION value = "Black Diamond" >Black Diamond
<OPTION value = "Bothell" >Bothell
<OPTION value = "Burien" >Burien
<OPTION value = "Carnation" >Carnation
<OPTION value = "Clyde Hill" >Clyde Hill
<OPTION value = "Des Moines" >Des Moines
</SELECT>

Thanks
Fred
Quote Reply
Re: Record Count In reply to
You need to define a select field in your .cfg file.

%db_select_fields = (
City => 'Algona,Auburn,Baring,Bellevue,Black Diamond,Bothell,Burien,Carnation,Clyde Hill,Des Moines'
);

Make sure they're all on one line, with no spaces before or after the commas between them.


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