Gossamer Forum
Home : Products : DBMan : Customization :

Weakness in List Categories and Give a Count?

Quote Reply
Weakness in List Categories and Give a Count?
Hi,

I'm about to set up a new database using DBMan. I love the program. I've installed the List Categories and Give a Count mod and I think I have discovered a weakness in the code. I'm not using the Validate Records mod. So, from the List Categories and Give a Count page I chose "Category names come from a select list definition in the .cfg file" - "Show only categories with matching records" - "For use without the "validate records" mod" - found at http://www.jpdeni.com/.../listcategories4.txt.

The database I'm about to launch is for a site about boats. Among others, I have 2 select fields. The first is named BoatManufacturer and have many select options, but the two select options causing me trouble are "Other" and "Albin". To avoid any confusion - "Albin" is a boat manufacturer. The other select field is named EngineManufacturer and also have many select options. This field also have select options "Other" and "Albin". There is also an engine manufacturer called "Albin", but they're not related to the boat manufacturer called "Albin". I also prefer to have an option called "Other" in such select lists because it's hard to cover all.

The problem is that it looks like the mod code mix up these two select fields when checking the database. It doesn't actually check which database field the keyword from the -.cfg definition is found, as long as it is found, it is listed and counted. E.g. when looking for the keyword "Albin" or "Other", found in the select definition for the field BoatManufacturer in the -.cfg file, the code will also count occurrences of these words in other database fields, e.g. the EngineManufacturer field. This will lead to categories being listed although they are empty and the number of records found is wrong.

What I'd like the code to do, is to make sure that the occurrence found in the database is in the same field number as the field number of the field to be listed and counted has in the -.cfg database setup.

I hope I'm making myself clear here. English is not my mother language.

Thank you.

Regards

Ole Knut Tobiassen

http://baatplassen.no

Norway
Quote Reply
Re: [oktrg500] Weakness in List Categories and Give a Count? In reply to
As I recall, you have some extra work to do if you are running categories on more than 1 column at the same time.

What happens, I think, is that the "results" of the first run through the categories sub are not "cleared" prior to the second run through.

You could put them in seperate subs with different variable names (which may be the simplest thing to do)

or

You could figure out when to clear the "results".
Quote Reply
Re: [joematt] Weakness in List Categories and Give a Count? In reply to
In Reply To:
As I recall, you have some extra work to do if you are running categories on more than 1 column at the same time.

What happens, I think, is that the "results" of the first run through the categories sub are not "cleared" prior to the second run through.

You could put them in seperate subs with different variable names (which may be the simplest thing to do)

or

You could figure out when to clear the "results".


I don't have a separate sub for this mod. Except the sub urlencode, all mod code is located in sub html_home. What I have done, is to run the mod first against one field. Already after the first run, I can see that the mod code has counted and listed records with the same words in them as in found in the select list definition for the field I'm running the mod against but the occurences of the keyword in these records are not in the field the code is looking for (Or should be looking for). After the mod code I've added the same code once again and this time against another field (This is a new listing).

This is the mod code

for ($i = 0; $i <= $#db_cols; $i++) {
if ($db_cols[$i] eq "Field1" ) {
$fieldnum = $i; $found = 1;
last;
}
}
if ($found) {
open (DB, "<$db_file_name") or &cgierr("couldn't 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;
@options = split (/\,/, $db_select_fields{'Field1'});
foreach $option (sort @options) {
if ($count{$option}) {
$encoded = &urlencode($option);
print qq|
<a href="$db_script_link_url&Field1=$encoded&view_records=1">$option</a> ($count{$option})<BR>
|;
}
}
}


Excample:

%db_def = ID => [0, 'numer', 5, 8, 1, '', ''],

Field1 => [1, 'alpha', 12, 255, 0, '', ''],

Field2 => [2, 'alpha', 12, 255, 0, '', ''],

And so on....

%db_select_fields = (

Field1 => 'Other,Albin,Sunseeker,Halco', (Boat manufacturer)

Field1 => 'Other,Albin,Yanmar,Yamaha', (Engine manufacturer)

sample database:

0|Sunseeker|Yanmar|

1|Other|Yamaha|

2|Halco|Other|

If I run the mod code against Field1 to List all Boat manufacturers with records and give a count, the mod will list

Halco (1)

Other (2) SHOULD LIST Other (1)

Sunseeker (1)

See the problem?

Thanks for your reply. Other suggestions?
Quote Reply
Re: [oktrg500] Weakness in List Categories and Give a Count? In reply to
Problem solved! What I wrote in my first post was partly not true. What I wrote about the mod code mixing up the database fields when checking the database was not true, it just looked that way. And further: What I wrote in my second post about the result being wrong already after the first run was also not true.Blush The problem occured first when I tried to run the same mod code later against another field. Then the second result was wrong. I spent some time studying the code and after a while when I finally understood what the code did, I saw the problem.

You probably was right in your post, joematt, I just diden't understand what you meant.

The problem was caused by this variable

++$count{$fields[$fieldnum]};

It wasn't cleared before the next run and thus it conained the result from the previous run. E.g. one of them being Other (1) from the sample database I posted above. During the next run the code found more records in the other field with the same select value (Other) and thus it just added to the result from the previous run.

What I did to fix it, was just rename all occurrences of $count, making sure that it had a different name for each run of the mod code.

Thank you very much joematt! You saved my day!

Last edited by:

oktrg500: May 7, 2003, 8:38 AM