Gossamer Forum
Home : Products : DBMan : Customization :

Record-view counter bugfixed HERE!

Quote Reply
Record-view counter bugfixed HERE!
Ok, here it is.

Record-view counter, bugfixed.

First, in your default.cfg add:
$db_file_count = $db_script_path . "/brojac.db";
$db_counter_field = 1;


In your default.cgi change :
------------------
open (ID, ">$db_id_file_name") or &cgierr("error in get_defaults. unable to open id file: $db_id_file_name.\nReason: $!");
if ($db_use_flock) {
flock(ID, 2) or &cgierr("unable to get exclusive lock on $db_id_file_name.\nReason: $!");
}
print ID $in{$db_key}; # update counter.
close ID; # automatically removes file lock
}

&auth_logging("added record: $in{$db_key}") if ($auth_logging);
&html_add_success;
---------------------------------

to:

---------------------
open (ID, ">$db_id_file_name") or &cgierr("error in get_defaults. unable to open id file: $db_id_file_name.\nReason: $!");
if ($db_use_flock) {
flock(ID, 2) or &cgierr("unable to get exclusive lock on $db_id_file_name.\nReason: $!");
}
print ID $in{$db_key}; # update counter.
close ID; # automatically removes file lock
}
# flag
open (CO, ">>$db_file_count") or &cgierr("error in get_defaults. unable to open id file: $db_file_count.\nReason: $!");
if ($db_use_flock) {
flock(CO, 2) or &cgierr("unable to get exclusive lock on $db_file_count.\nReason: $!");
}
print CO $in{$db_key}.$db_delim."1"; # update counter.
close CO; # automatically removes file lock


&auth_logging("added record: $in{$db_key}") if ($auth_logging);
&html_add_success;
--------------------------------


Then, in html.pl (html_record or html_record_long) after
----------------
my (%rec) = @_; # Load any defaults to put in the VALUE field.
($db_auto_generate and print &build_html_record(%rec) and return);
$rec{$db_key} =~ s/<.?B>//g;
my $font_color = 'Font face="Verdana, Arial, Helvetica" Size=2 Color=#33ff33';
my $font = 'Font face="Verdana, Arial, Helvetica" Size=2 Color=#CCFFCC';
-------------------
add:
--------------
my $broj = '0';
open (CO, "<$db_file_count")
or &cgierr("error in modify_records. unable to open db file: $db_file_count.\nReason: $!");
if ($db_use_flock) { flock(CO, 1); }
@lines = <CO>; # Slurp the database into @lines..
close CO;
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}) {
$broj = $data[$db_counter_field];
}

}

and at the end of the same sub (html_record or html_reocrd_long) add:
---------------------
open (DB, "<$db_file_count")
or &cgierr("error in modify_records. unable to open db file: $db_file_count.\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_count")
or &cgierr("error in modify_records. unable to open db file: $db_file_count.\nReason: $!");
if ($db_use_flock) {
flock(DB, 2) or &cgierr("unable to get exclusive lock on $db_file_count.\nReason: $!");
}
print DB $output;
close DB;

---------------------

Make empty file called brojac.db (or anything you specified in default.cfg) and that's it.

Maybe it's a kind messy and probably it could be done much better, but this works just fine.

Quote Reply
Re: Record-view counter bugfixed HERE! In reply to
This does not seem to work for me. I added all the codes to my default.cgi and html.pl files. I also added the following:

1) New Field in the db_def array named Hits. Here are the configurations:

Code:
Hits => [26, 'numer', 8, 8, 0, '',
'']

I set the following these configurations for the new counter mod in default.cfg:

Code:
$db_counter_field = 26;

2) I added the following codes to the record_long to "display" the counter:

Code:
<tr>
<td width="50%" bgcolor="EEEEEE">
<strong><$font>Number of Hits:</font></strong></td>
<td width="50%">
|;
if ($rec{'Hits'} gt $db_default{'Hits'}) {
print qq|<$font>$rec{'Hits'}</font></td></tr>|;
}
else {
print qq|  </td></tr>|;
}
print qq|

The Hit Counter does not grow. The hits stay the same at 1.

Also, I noticed that the hits.db file I have has a bunch of machine language in it. In addition, it seems that the whole regular .db file is copied over into the hits.db file. They are same size. This does not seem to be a good fix for the hit counter if the alternative database file (hits.db) grows at the same pace and size as the regular database file.

Any help with this is greatly appreciated. But, Cacan, I do appreciate the effort you put into this mod. Seems like it needs fine-tuning, but it is getting there.

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 August 07, 1999).]

[This message has been edited by Eliot (edited August 07, 1999).]
Quote Reply
Re: Record-view counter bugfixed HERE! In reply to
Hi Eliot,

I used different approach for this mod, so it only looks like previous one.

This one uses counter.db because of 255 characters limit per line.

First of all, do only what is said here, forget previous versions :-), and delete counter field in default.cfg.

In default.cfg you only have to add:
$db_file_count = $db_script_path . "/brojac.db";
$db_counter_field = 1;

And, $db_counter_field = 1; must be 1.

Another thing I haven't said in my first post is where you want hits to be displayad don't use $rec{'Hits'}, but $broj .

Follow other steps, and it should work...

Does it work now ?!
Quote Reply
Re: Record-view counter bugfixed HERE! In reply to
Nope...It still doesn't work. I made the changes you specified in the precedding post. The hit count for the records stays at 0.

I put these lines in the default.cfg file:

Code:
$db_file_count = "/mnt/web/guide/anthrotech/cgibin/classifieds/data/oppshits.db";
$db_counter_field = 1;

I also used $hitcounter rather than $rec{'Hits'} in the html_record_long. (I changed your $broj value to $hitcounter.)

I noticed that your hit counts starts when a record is added. I am assuming that this does not work with old records in the database prior to installing this mod. Do you have any suggestions for placing a hit counter for old records in my database??

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
Quote Reply
Re: Record-view counter bugfixed HERE! In reply to
Hi Eliot,

Have you changed all $broj to $hitcounter in html.pl and default.cgi ?!?

It's true, it doesn't work with previously added fileds, but that could be easily fixed.

Write script that will in your oppshits.db write something like:
record_ID|0

or if you have deleted some records you could create a script that will read all record_IDs and in oppshits.db write record_ID|0 .

Hope this helps.


[This message has been edited by Cacan (edited August 08, 1999).]
Quote Reply
Re: Record-view counter bugfixed HERE! In reply to
Hi Eliot,

Have you changed all $broj to $hitcounter in html.pl and default.cgi ?!?

It's true, it doesn't work with previously added fileds, but that could be easily fixed.

Write script that will in your oppshits.db write something like:
record_ID|0

or if have deleted some records you could create a script that will read all record_IDs and in oppshits.db write record_ID|0 .

Hope this helps.
Quote Reply
Re: Record-view counter bugfixed HERE! In reply to
Yes, I did replace all $broj variables with $hitcounter. I tried adding a new record and the hit counter stays at 0 in the web page. In the oppshits.db file, I get the following result:

Code:
8|1

But when I try reloading the page and also accessing the record as a different user, the number stays at 0 in the web page and the record stays the same in the oppshits.db file.

Also, there isn't a $broj variable in the default.cgi modifications you've provided.

Here is what I have in the default.cgi based on the codes you gave:

Code:
open (DB, ">>$db_file_name") or &cgierr("error in add_record. unable to open database: $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 &join_encode(%in);

# automatically removes file lock

close DB;
if ($db_key_track) {
open (ID, ">$db_id_file_name") or &cgierr("error in get_defaults. unable to open id file: $db_id_file_name.\nReason: $!");
if ($db_use_flock) {
flock(ID, 2) or &cgierr("unable to get exclusive lock on $db_id_file_name.\nReason:
$!");
}
# update counter.

print ID $in{$db_key};

# automatically removes file lock

close ID;
}
# flag
open (CO, ">>$db_file_count") or &cgierr("error in get_defaults. unable to open id
file: $db_file_count.\nReason: $!");
if ($db_use_flock) {
flock(CO, 2) or &cgierr("unable to get exclusive lock on $db_file_count.\nReason:
$!");
}
# update counter.

print CO $in{$db_key}.$db_delim."1";

# automatically removes file lock

close CO;
&auth_logging("added record: $in{$db_key}") if ($auth_logging);

Am I missing something here??

I will try to hack the script that will add a hit counter to all records even if they have been added prior to installing this mod. This will be a nice add-on to your mod, since many people may add this mod after their database is live. But, first I would like to get this mod functioning properly.

Any assistance is greatly appreciated.

------------------
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 August 08, 1999).]

[This message has been edited by Eliot (edited August 08, 1999).]

[This message has been edited by Eliot (edited August 08, 1999).]
Quote Reply
Re: Record-view counter bugfixed HERE! In reply to
Hmmm, let me see...

Obviously, adding is no problem, it works.
In your example 8 is db_key and 1 is hitcounter counter value.

Problem is in updatig the counter, and updating is done in html.pl part.


Maybe the problem is in flocking, so in default.cfg set flock to 0.

There are two pieces of code that need to be insterted in html.pl. On goes right after the initialisation of variables in html_record (or html_record_long) and is used for reading the hitcounter for a given db_key (ID), so it only reads, doesn't write anything.

And, other part (shown below) is used for updating the cunter.


Please, triple check this part in html.pl.

open (DB, "<$db_file_count")
or &cgierr("error in modify_records. unable to open db file: $db_file_count.\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_count")
or &cgierr("error in modify_records. unable to open db file: $db_file_count.\nReason: $!");
if ($db_use_flock) {
flock(DB, 2) or &cgierr("unable to get exclusive lock on $db_file_count.\nReason: $!");
}
print DB $output;
close DB;


Quote Reply
Re: Record-view counter bugfixed HERE! In reply to
I inserted those codes in html.pl in the html_record_long sub-routine in the exact spots you mentioned...no go! Smile

Any other thoughts??

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
Quote Reply
Re: Record-view counter bugfixed HERE! In reply to
Put somewhere your html.pl where I could get it.

Quote Reply
Re: Record-view counter bugfixed HERE! In reply to
Cacan,

Here are codes I have in the html_record_long sub routine:

Code:
my $hitcounter = '';
open (CO, "<$db_file_count") or &cgierr("error in modify_records. unable
to open db file:$db_file_count.\nReason: $!");

if ($db_use_flock) { flock(CO, 1); }
@lines = <CO>; # Slurp the database into @lines..

close CO;

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}) {
$hitcounter = $data[$db_counter_field];
}
}

Then at the end of the sub-routine, I have the following:

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

open (DB, "<$db_file_count") or &cgierr("error in modify_records. unable
to open db file: $db_file_count.\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_count") or &cgierr("error in modify_records. unable t
o open db file: $db_file_count.\nReason: $!");
if ($db_use_flock) {
flock(DB, 2) or &cgierr("unable to get exclusive lock on
$db_file_count.\nReason: $!");
}
print DB $output;
close DB;

I am calling the $hitcounter in the following HTML codes, which is part of a table:

Code:
<tr>
<td width="50%" bgcolor="EEEEEE">
<strong><$font>Number of Hits:</font></strong></td>
<td width="50%">
<$font>$hitcounter</font></td></tr>

Still not working. Even when I kept the following line like you had it:

Code:
my $hitcounter = '0';

I changed it to '' thinking that it needed to have the number inserted from the oppshits.db.

I checked the codes more than three times and still no go. Frown

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
Quote Reply
Re: Record-view counter bugfixed HERE! In reply to
Sorry Eliot, but it goes beyond my knowledge... :-(


Maybe Carol could help.
Quote Reply
Re: Record-view counter bugfixed HERE! In reply to
Thanks, Cacan, for your effort. May be others will be able to clean up your codes and provide a solution.

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
Quote Reply
Re: Record-view counter bugfixed HERE! In reply to
This is the output I am getting in the hits database file:

Code:
8|19|110|1
11|2

May be somebody can read this Thread and identify the bugs and assist us with coming up with a truly clean counter program.

Thanks.

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
Quote Reply
Re: Record-view counter bugfixed HERE! In reply to
Hi Eliot,

Finally I have tested mod on both unix and win32 machine, and everything works fine. I really don't know where the problem is with your mod.

Quote Reply
Re: Record-view counter bugfixed HERE! In reply to
Well, I have come up with another mod that works a lot better and does not include modifying existing sub-routines in the db.cgi file. I created another Thread for my mod.

Thanks anyway.

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