Gossamer Forum
Home : Products : DBMan : Customization :

Average and image fields

Quote Reply
Average and image fields
Hello,

I have a database setup in dbman where the user answer's questions relating to finances. What I would like to do is:

Field 1: Numeric value entered by user

Field 2: Average of all answers to Field 1

Field 3: A graphic that shows if the answer to Field 1 is Above, Below, or even with the Average in Field 2.

Any help would be appreciated.

Buddy
Quote Reply
Re: [Budb] Average and image fields In reply to
You wouldn't need to keep the data from Fields 2 & 3 in the database, would you? Those values would change as more records were added. They would just be in the display.

Your database would consist of a counter for the key (probably), Field 1 as you described it, and anything else that you would want to enter for the record.

When would this information be displayed? Would it be just when a new record is added or would it be in search results?

Could you give a little more info about what you're trying to do?


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.
Quote Reply
Re: [JPDeni] Average and image fields In reply to
This a database of answers from financial brokers across the country. They answer questions such as:

What are your assets under management for 2005?

You can see a pdf sample of the results we are after at http://www.buddydigital.com/Survey.pdf.

The answer they entered is in the Your Answer column.

The Nat'l Avg column looks at everyone in the db who answered that question and returns the average.

The last column returns an up arrow if their answer is over the Nat'l average;
A down arrow if their answer is below the Nat'l Average and;
and even arrow if their answer is the same.

Hope this helps and I appreciate you help.

Buddy
Quote Reply
Re: [Budb] Average and image fields In reply to
That helps a lot!

You'll want to set up your database with a userid field (so they can view their own stats) and a field for each of your questions. If a user might want to have more than one record, you'd also need a counter field for the key, but it's likely that you can just use the userid field as the key.

I thought about this quite a bit and I think the best way to handle it is to keep the averages in a separate file. It would have to be re-generated every time a record was added, modified or deleted, but it would lessen the load on the server and save a whole lot of time when just viewing records.

The first thing would be to create the averages file. You'd need to set up a new subroutine in db.cgi that would go through the database and collect and count the data from each of the records, then compute the averages of each field and write them to a file. You'd make a call to this subroutine in the add, modify and delete routines, after it was established that an error hadn't been made. (I just got up and I'm not real good with words yet. I hope that made sense. :-) )

Then, in html.pl in sub html_record, you'd open the file with the averages and load the data into variables. Then it would simply be a matter of printing out those variables and doing a little comparison to decide which graphic to use.

I don't know how much help you need. Some folks just need a nudge to start them in the right direction and can do the programming themselves. Others need to have the programming done for them. I'll do a bit of stuff now, but wait until I hear from you before I post it.


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.
Quote Reply
Re: [JPDeni] Average and image fields In reply to
Hi JP,

I'm not too bad with DBman although I've always only stuck to the basics: setup, adding mods etc. The areas I would need help with are linking the two databases, and getting the averages in a separate file (including the subroutine that did that).

A little more description of how the program works might help:

A user enters a site (outside DBMAN) via a username and password.

When he goes to the survey page, he has the opportunity to click on a link and view or modify his record in DBMAN based on his key. (All the records have already been created in DBMan, the answers are just blank).

Only the administrator will be creating and deleting records.

The users can only modify his own record, but doesn't get to it through DBMan, he clicks on a link from the source site. (so the DBMan menus are only available per Admin)

Thanks for your time.
Quote Reply
Re: [Budb] Average and image fields In reply to
Hey Bud --

Quote:
All the records have already been created in DBMan, the answers are just blank

That helps a lot.

It looks like there are going to be 6 fields of actual data, as well as the username. I think I'm probably going to have to hard-code the position of those fields into the program. At the moment, I've got them listed as the first 6 fields and any other fields would have to be after that. It can be changed, though. The real important thing is that they are contiguous. Whether they're fields 0-5 or 1-6 or 53-58 doesn't matter too much. They just can't be fields 0-3 and 8-9.

This is what I have so far. I haven't tested it as far as whether it actually works Smile, but it doesn't have any syntax errors.

Add the following subroutine to db.cgi:

Code:
sub compute_averages {
my ($line, @lines, @average, @total, $number_of_records, $output, @data);

open (DB, "<$db_file_name") or &cgierr("error in modify_records. unable to open db file: $db_file_name.\nReason: $!");
if ($db_use_flock) { flock(DB, 1); }
@lines = <DB>; # Slurp the database into @lines..
close DB;

LINE: foreach $line (@lines) {
if ($line =~ /^$/) { next LINE; } # Skip and Remove blank lines
if ($line =~ /^#/) { $output .= $line; next LINE; } # Comment Line
chomp ($line);
@data = &split_decode($line);
if ($data[0] > 0) {
++$number_of_records;
for ($i = 0; $i<=5; $i++) {
$total[$i] += $data[$i];
}
}
}

if ($number_of_records > 0) {
for ($i = 0; $i<=5; $i++) {
$average[$i] = $total[$i] / $number_of_records;
}
$output = join '|', @average;
}

open (AVG, "$avg_file_name") or &cgierr("error. unable to open average file.\nReason: $!");
if ($db_use_flock) {
flock(AVG, 2) or &cgierr("unable to get exclusive lock on average file.\nReason: $!");
}
print AVG $output;
close AVG; # automatically removes file lock
}


Note that there is no error-checking. If they enter something that isn't a number, it's going to cause problems. You'll need to check for that elsewhere.

Add
$avg_file_name = $db_script_path . "/average.db";
to your .cfg file after
$db_file_name = $db_script_path . "/default.db";

Add
&compute_averages;
to db.cgi
sub modify_record after &auth_logging("modified record: $in{$db_key}") if ($auth_logging);
sub delete_records
after &auth_logging("deleted records: $succstr") if ($auth_logging);

You won't need to put it in the add records routine because the records are empty when added and won't change the totals.

As for opening and reading the file, put this at the beginning of sub html_record:

Code:
open (AVG, "<$average_file_name") or &cgierr("error in html_record. unable to open average file.\nReason: $!");
if ($db_use_flock) { flock(AVG, 1); }
$line = <AVG>; # Slurp the database into @lines..
close AVG;

@average = split (/|/, $line);


The averages will now be in an array and will be in the same order you have the fields listed in your database. Access the amounts using the variables $average[0], $average[1], $average[2], etc.

As far as the graphic goes, it's just a matter of comparing the field in the individual record with the corresponding average. A series of "if" statements should do it. And I'll be pleased to help you with those, too, but I wanted to send this off and let you play with it before I worked on that. Sly

Let me know if there are any problems. I've checked the syntax and it all seems logical, but you never know when something might not be right.


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.
Quote Reply
Re: [JPDeni] Average and image fields In reply to
Thanks JP,

I'll have to give this a shot a little later this afternoon, but am excited to try it.

I'll get back to you.
Buddy
Quote Reply
Re: [JPDeni] Average and image fields In reply to
Ok, I get stuck when I modify record with:

CGI ERROR
==========================================
Error Message : error in modify_records. unable to open db file: ./default.db.
Reason: No such file or directory
Script Location : db.cgi
Perl Version : 5.006001
Setup File : default.cfg
User ID : admin
Session ID : admin.111860597096675


I did create a blank average.db file.

Here's all the files if you need to look at them: http://www.buddydigital.com/dbman.zip


Here's modify record:
sub modify_record {
# --------------------------------------------------------
# This routine does the actual modification of a record. It expects
# to find in %in a record that is already in the database, and will
# rewrite the database with the new entry. First it checks to make
# sure that the modified record is ok with validate record.
# It then goes through the database looking for the right record to
# modify, if found, it prints out the modified record, and returns
# the user to a success page. Otherwise the user is returned to an error
# page with a reason why.
my ($status, $line, @lines, @data, $output, $found, $restricted);

$status = &validate_record; # Check to make sure the modifications are ok!
if ($status eq "ok") {
open (DB, "<$db_file_name") or &cgierr("error in modify_records. unable to open db file: $db_file_name.\nReason: $!");
if ($db_use_flock) { flock(DB, 1); }
@lines = <DB>; # Slurp the database into @lines..
close DB;
($restricted = 1) if ($auth_modify_own and !$per_admin);
$found = 0; # Make sure the record is in here!
LINE: foreach $line (@lines) {
if ($line =~ /^$/) { next LINE; } # Skip and Remove blank lines
if ($line =~ /^#/) { $output .= $line; next LINE; } # Comment Line
chomp ($line);
@data = &split_decode($line);
($output .= "$line\n" and next LINE) if ($restricted and ($db_userid ne $data[$auth_user_field]));

if ($data[$db_key_pos] eq $in{$db_key}) {
# If we have userid's and this is not an admin, then we force the record to keep it's own
# userid.
if ($auth_user_field >= 0 and (!$per_admin or !$in{$db_cols[$auth_user_field]})) {
$in{$db_cols[$auth_user_field]} = $data[$auth_user_field];
}
$output .= &join_encode(%in);
$found = 1;
}
else {
$output .= $line . "\n"; # else print regular line.
}
}
if ($found) {
open (DB, ">$db_file_name") or &cgierr("error in modify_records. unable to open db file: $db_file_name.\nReason: $!");
if ($db_use_flock) {
flock(DB, 2) or &cgierr("unable to get exclusive lock on $db_file_name.\nReason: $!");
}
print DB $output;
close DB; # automatically removes file lock
&auth_logging("modified record: $in{$db_key}") if ($auth_logging);
&compute_averages;
&html_modify_success;
}
else {
&html_modify_failure("$in{$db_key} (can't find requested record)");
}
}
else {
&html_modify_failure($status); # Validation Error
}
}

Did I miss a step?

Thanks
Quote Reply
Re: [Budb] Average and image fields In reply to
You did already test DBMan by adding and modifying records, right? The error you posted is about the default.db file and not the average.db file.

I would change one line in the subroutine just so we know where the error is for certain.

Change

open (DB, "<$db_file_name") or &cgierr("error in modify_records. unable to open db file: $db_file_name.\nReason: $!");

to

open (DB, "<$db_file_name") or &cgierr("error in computing averages. unable to open db file: $db_file_name.\nReason: $!");


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.
Quote Reply
Re: [JPDeni] Average and image fields In reply to
Yes, I did test it. 3 recods have been added. On adding the 3rd record, I go the same error.

CGI ERROR
==========================================
Error Message : error in html_record. unable to open average file.
Reason: No such file or directory
Script Location : db.cgi
Perl Version : 5.006001
Setup File : default.cfg
User ID : admin
Session ID : admin.111861103810403

Form Variables
-------------------------------------------
A : 30
B : 60
C : 90
D : 20
E : 10
F : 50
ID : 73
Username : admin
add_record : Add Record
db : default
uid : admin.111861103810403


You can see it at http://www.powerinterchange.com/cgi/dbman/test/db.cgi
Quote Reply
Re: [Budb] Average and image fields In reply to
Did the problem with modifying records work out?

I did forget a little thing. You do need to create the average.db file and chmod it to 666. That could be the problem.

You'll need to modify a record in order to rebuild the average.db file.

... Wait. You said you had already created all the records, didn't you? That's why I didn't have you add the call to the computing averages subroutine within the add records routine.


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.
Quote Reply
Re: [JPDeni] Average and image fields In reply to
Still get the same error. I went back and took out all the code and was able to view/add/modify records. When I added it back, I got the same error pointing to html record.

Could it be here:

open (AVG, "<$average_file_name") or &cgierr("error in html_record. unable to open average file.\nReason: $!");
if ($db_use_flock) { flock(AVG, 1); }
$line = <AVG>; # Slurp the database into @lines..
close AVG;

@average = split (/|/, $line);

Quote Reply
Re: [Budb] Average and image fields In reply to
You had a problem when you were modifying records. Did that problem get fixed?

You really need to modify a record before you do anything else so that there is something in the average.db file.


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.
Quote Reply
Re: [JPDeni] Average and image fields In reply to
If I take out the code at the top of the html_record, I can modify just fine. If I add it back, it says the record has been modified although all I see is the debugging info, not the record. You can see what I mean at http://www.powerinterchange.com/dbman/test/db.cgi U:admin P:admin

Thanks
Quote Reply
Re: [Budb] Average and image fields In reply to
Your link gives me a "file not found" error.

Does the average.db file get written when you modify a record?


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.
Quote Reply
Re: [JPDeni] Average and image fields In reply to
The average.db file is not being updated.
Sorry the link was http://www.powerinterchange.com/cgi/dbman/test/db.cgi
Quote Reply
Re: [Budb] Average and image fields In reply to
I see a problem. I don't know how it happened, but I made an error.

In the sub compute_averages, the line

open (AVG, "$avg_file_name") or &cgierr("error. unable to open average file.\nReason: $!");

should be

open (AVG, ">$avg_file_name") or &cgierr("error. unable to open average file.\nReason: $!");

See if that will update your average.db file.


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.
Quote Reply
Re: [JPDeni] Average and image fields In reply to
Sorry, I get the same error. The page display's record modified, and the default.db file is modified although the average.db file does not change and the record will not view. I only see the error:

CGI ERROR
==========================================
Error Message : error in html_record. unable to open average file.
Reason: No such file or directory
Script Location : db.cgi
Perl Version : 5.006001
Setup File : default.cfg
User ID : admin
Session ID : admin.111862062585823

I went back and redid everything from a fresh install but the results are the same.

Buddy
Quote Reply
Re: [Budb] Average and image fields In reply to
I'm not sure what to tell you, then. The only thing I can think to do is to install a version of DBMan in my own webspace and try to get what you want. If I have time today and tomorrow, I'll give it a try.


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.
Quote Reply
Re: [JPDeni] Average and image fields In reply to
Thanks for your time JP. I'll go back to square one and try your solution again as well and see if I missed anything.

Buddy
Quote Reply
Re: [Budb] Average and image fields In reply to
I did find a few errors I had made. (I'm comforted by the probably apocryphal story that Albert Einstein had problems with algebra. Smile)

Take a look at http://www.jpdeni.com/...bin/dbmantest/db.cgi

I think that'll give you what you want. You're welcome to add, delete, modify or whatever. U: admin P: admin

There are a few things you might notice.

The averages are rounded to the nearest integer.
You must enter numbers without any dollar signs, commas or per cent signs, but they are added when the numbers are printed out.

There's a problem with you, as admin, adding records and then having users modify their own. If you set a userid field, then the userid of whoever adds the record is automatically entered in the field, no matter what you type. So, if you add records through the DBMan interface, the userid field will always say "admin." If you want users to log in and modify their own records that you have added, you'll need to create the records in the .db file on your own. It's not hard. You just use

||||||userid|counter

on each separate line. You can update the counter file, too, if you think you might be adding records through the DBMan interface.

At the moment, I have set the userid field to -1 so that you can add records without the program changing the userid field. I suppose you could "comment out" the line that changes the userid field in sub add_record.

I can either post the changes I made here or send you the whole kit and kaboodle in a .zip file. Whichever you wish.


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.
Quote Reply
Re: [JPDeni] Average and image fields In reply to
That's perfect JP. I think it would be good to link to the zip file from here. I bet lots of users can get benifit from this. I searched for quite a while before I asked the question.

Thanks so much for your time.

Buddy
Quote Reply
Re: [Budb] Average and image fields In reply to
Okey-dokey. The whole thing, including the records I made and the average.db file is available at http://www.jpdeni.com/dbmantest.zip .

There are also some changes to the .cfg file. Some minor stuff, but take a look at it.


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.

Last edited by:

JPDeni: Jun 13, 2005, 9:51 AM