Gossamer Forum
Home : Products : DBMan : Customization :

List Categories & Count Mod - need "English translation" of 1 line of code

Quote Reply
List Categories & Count Mod - need "English translation" of 1 line of code
I am trying to modify the List Categories & Count Mod. I have it working beautifully for one field, but for my second attempt I'm trying to sort by one field (author_last) and display by another (author_full). I have a lovely theory on how I'm going to do this (2 or 3 have already failed) but I'm having trouble figuring out what's going on in one of these lines so that I can tinker with it.

Here's the actual code.

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[$fieldnum]};
}
close DB;

##Here's my loose translation of what's happening

## If the field to sort by exists, then...
##open the .db file
## and, if possible, lock the file.
## While there's still more stuff in the .db file, grab the next line.
## If the line starts with #, go on to the next line.
## Likewise, if the line starts with whitespace, go on to the next line.
## Otherwise, assign the line to the variable $line,
## and trim off the unwanted stuff at the end.
## Let the subroutine &split_decode break the line into fields and assign them to the fields array.

## What does this do? ++$count{$fields[$fieldnum]};

I know that count is a hash, and that *something's* getting incremented, but I can't see what's being put into the hash.

I *think* what I want in this hash is author_last keyed to author_full. Then I *should* be able to figure out how to sort by author_last and display by author_full. But since I don't know what's happening now, I don't know how to tinker with it...

Thanks for any help.

Kristen Skold
Quote Reply
Re: [kskold] List Categories & Count Mod - need "English translation" of 1 line of code In reply to
What version of the mod are you using? Is it 'Category names come from records'

There was a thread which addressed something similiar and is in the FAQ under the title "List Users and number of records (sub html_author_list)"

Although this was written with the relational mod in mind, I'm sure it will provide ideas to find a solution for your needs.

Unoffical DBMan FAQ
http://redundantcartridge.com/dbman/
Quote Reply
Re: [LoisC] List Categories & Count Mod - need "English translation" of 1 line of code In reply to
Yes, this was originally "Category names come from the records"

I decided I didn't need the count element, so I just removed the line I couldn't decipher and hit the books. This version doesn't display a count. It sorts by one field, and displays another. I used it for sorting by author's last name and displaying author's full name.

It would need a similar set-up to work for someone else. All the records with "Veryan" in author_last have "Patricia Veryan" in author_full. I created the "author_last" field specifically for sorting by. Where I have two authors with the same last name, I added their first initials - ScottA and ScottR.

I'm posting the mod in case someone else can use it. I've always found posting questions to be useful - about half the time I get a great answer and half the time just writing out the question helps clarify the problem enough to find a solution. (Working before midnight sometimes helps too<g>)

I'd still love to know what that one line of code does...

Kristen Skold
http://www.regencylady.com/.../db.cgi?db=bookstall

####This mod of the List Categories and Give a Count Mod *does not count*
####What it does is sort by one field and display another.
####I used it to sort by author's last name, but display author's full name.
####(I have two fields for this - author_last and author_full)



####################################
#
#Add the following to db.cgi, somewhere near the end:
#
####################################

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


#####################################
#
# Please read carefully all lines that begin with ####.
#
# On the page where you wish to print out your categories -- probably
# in sub html_home, add
#


for ($i = 0; $i <= $#db_cols; $i++) {
#### In the line below, replace 'Category_sort' with the name of the field you want to sort by.
if ($db_cols[$i] eq "Category_sort" ) {
$fieldnumsort = $i; $foundsort = 1;
last;
}
}

for ($i = 0; $i <= $#db_cols; $i++) {
#### In the line below, replace 'Category_display' with the name of the field you want to display.
if ($db_cols[$i] eq "Category_display" ) {
$fieldnumdisplay = $i; $founddisplay = 1;
last;
}
}

if ($foundsort && $founddisplay) {
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);
$authorlist{$fields[$fieldnumsort]} = $fields[$fieldnumdisplay];
}
close DB;

foreach $option (sort keys %authorlist) {
$encoded = &urlencode($option);
#### In the line below, replace 'Category_display' with the name of your display field.
print qq|
<a href="$db_script_link_url&Category_display=$encoded&view_records=1">$authorlist{$option}</a><br>|;
}
}