Gossamer Forum
Home : Products : DBMan : Customization :

Ask for counter for each record

Quote Reply
Ask for counter for each record
Hello all

I want setup counter for each record.
If the record is search out, it will add 1 into the counter.

Thx

Gab

------------------
Quote Reply
Re: Ask for counter for each record In reply to
A number of people have asked for this, but I'm not sure how to implement it. It was tried once, but caused great problems with the script.


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





Quote Reply
Re: Ask for counter for each record In reply to
That is not a great solution for those people that want to have bolded search results.

------------------
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
Quote Reply
Re: Ask for counter for each record In reply to
The problems script caused was because because BOLDing should be turned off.

Here is counter script, put it in your html_record or html_record_long in your html.pl file. Just be sure to set
$db_bold = 0; in your default.cfg.

And, add another field in your database (I call it Brojac).

As I can see, the problem with this code it that reads all fileds, and write them back. Why doesn't it read only Broajc field and change only Brojac field ?!?


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; }
if ($line =~ /^#/) { $output .= $line; next LINE; }
chomp ($line);
@data = &split_decode($line);
if ($data[$db_key_pos] eq $rec{$db_key}) {
++$rec{'Brojac'};
$output .= &join_encode(%rec);
}
else {
$output .= $line . "\n";
}
}
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;
Quote Reply
Re: Ask for counter for each record In reply to
This might work, with a little bit of a change.

Instead of

Code:
@data = &split_decode($line);
if ($data[$db_key_pos] eq $rec{$db_key}) {
++$rec{'Brojac'};
$output .= &join_encode(%rec);

Try this:

First set a variable -- call it $db_counter_field and set it to the number of the field that your counter is in. Put this in your default.cfg file, like this:

$db_counter_field = 4;

Code:
@data = &split_decode($line);
if ($data[$db_key_pos] eq $rec{$db_key}) {
++$data[$db_counter_field];
$output .= join($db_delim, @data) . "\n";

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





Quote Reply
Re: Ask for counter for each record In reply to
Thx all
Quote Reply
Re: Ask for counter for each record In reply to
Hello JPDeni

by your changed code, can I set the $db_bold=1 ?

Thx
Quote Reply
Re: Ask for counter for each record In reply to
You should be able to, since it doesn't write anything extra to the database, other than the updated counter.

BTW, no one has mentioned yet whether this works. I have no way of testing it myself, since I don't have a database set up like this. I would really appreciate knowing whether things work or not.

It's nice for people to thank me, and I like that very much. But, if you have a choice between thanking me and telling me whether or not it works, I'd appreciate the latter.
------------------
JPD







[This message has been edited by JPDeni (edited July 07, 1999).]
Quote Reply
Re: Ask for counter for each record In reply to
Doesn't seem to work. I copied the codes and pasted them into the html_record_long sub-routine of the html.pl file. I did use JPDeni's suggestion and created the db_counter_field and also added a field, "Counter" to the default.cfg file.

The codes do not seem to display a counter on the record. How do you call the counter to appear on the record display page??

I used $rec{'Counter'}.

But at least the codes did not crash my database file with bolded codes all over the place. Smile

------------------
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 July 07, 1999).]
Quote Reply
Re: Ask for counter for each record In reply to
Did you look in your .db file to see if the counter was working? Did you start with a blank .db file so that all of the records would have the new fields?

You might need to add a default value of '0' to the Counter field.

Then again, I don't know if it will work or not. This was merely a guess.


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





Quote Reply
Re: Ask for counter for each record In reply to
JPDeni,

Hi there. I looked at my .db file and some weird stuff was happening (no <B> tags, TG).
But in one of the records, I added a number to the Counter field. Then I looked at the
.db file...I got two 1's in the entry.

.....|1|1

(But the other records remained in tact and there were no additional fields added.)

Very odd, indeed.

I did not start with a blank database file. I will test that and see what happens.

Regards,

------------------
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 July 08, 1999).]
Quote Reply
Re: Ask for counter for each record In reply to
JPD, sorry for a little bit late answer... I had some final exams at my uni...

Your code works with me just fine... I've tested it with bold=on and it works...

tnx...

Quote Reply
Re: Ask for counter for each record In reply to
Great! Thanks for letting me know. Smile


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





Quote Reply
Re: Ask for counter for each record In reply to
Hi Folks...clarification please!!!
will the above code change count the number of times each individual record is viewed or is it a counter for all records viewed...if it's the latter what code is required for the former???? thanks Novice
Quote Reply
Re: Ask for counter for each record In reply to
It will count the number of times each individual record is viewed.

Maybe I ought to recap it for you, just so everything's together.

First, you need to add a field to hold the count -- you could call it "Counter." Smile

Then set a variable -- call it $db_counter_field and set it to the number of the field that your counter is in. Put this in your default.cfg file, like this:

$db_counter_field = 4;

Now, add the following to html_record, just after

my (%rec) = @_;

Code:
open (DB, "<$db_file_name") or &cgierr("error. 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; }
if ($line =~ /^#/) { $output .= $line; next LINE; }
chomp ($line);
@data = &split_decode($line);
if ($data[$db_key_pos] eq $rec{$db_key}) {
++$data[$db_counter_field];
$output .= join($db_delim, @data) . "\n";
}
else {
$output .= $line . "\n";
}
}
open (DB, ">$db_file_name") or &cgierr("error. 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;

This will work even if you use the autogenerate feature. If you are using the short/long display mod, it probably would be better to put this in html_record_long. If you don't know what I'm talking about in the previous sentence, just ignore it. Smile

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







[This message has been edited by JPDeni (edited July 09, 1999).]