Gossamer Forum
Home : Products : DBMan : Customization :

View counter on long format record...

Quote Reply
View counter on long format record...
Has anybody integrated a simple view counter into db.cgi that would write to a new field how many times the long format record has been displayed? I would then like to use that information to generate a list of the X most popular records ranked in descending order of page views...

------------------
The Immuatable Order of Modding
-=-=-=-=-=-=-=-
1. Read the FAQ, 2. Search the board, 2a. Search the board again, 3. ask the question, 4. back-up, 5. experiment, 6. rephrase question (or better yet, post solution to original question)

[This message has been edited by oldmoney (edited January 20, 2000).]
Quote Reply
Re: View counter on long format record... In reply to
Hey oldmoney....

Carol was working on a hit program awhile back. Also, I came up with a hit counter that incremented in a very chaotic manner that ignored browser reloads.

Another DBMAN user also came up with a counter mod as well.

I think this was worked on during the late summer 1999 or early fall 1999.

I know the search utility is less to be desired in these forum...but you might find related Threads.

Wink

Regards,

------------------
Eliot Lee
Anthro TECH,L.L.C
http://www.anthrotech.com
Be sure to visit the Resource Center for FAQ's, Modifications and Extra Goodies!!
----------------------





Quote Reply
Re: View counter on long format record... In reply to
Trust me, I tried the search route... and flailed! I just found your thread, www.gossamer-threads.com/scripts/forum/resources/Forum12/HTML/000710.html

re: your hit counter, which I have given just a cursory glance over. It looks like it builds one hit count file, as opposed to modifying the db in real-time (which would be a pain). Did you look into building separate hit counter files? I can see advantages and disadvantages of both routes, as well as a third option of building temporary hit counter files that get incorporated into the DB on a re-build (like nph in Links).

I think the $ENV{'QUERY_STRING'} is a non-starter because DBMan never references the actual ID in a search (only the placeholder nh=X). The obvious solution is to pass the $rec{'ID'} to the hitcount sub... it should work in theory, but maybe you already tried that. As for the reload problem, you might be able to use the referrer environment variable, but then going to another page and coming right back would be another count. I think the better solution would be to use a cookie that expires with the browser session (e.g. no expiration date), or you could use IP address logging if you were going the temp hit counter file route. I haven't started playing with it yet, so any advice on where I might stumble would be appreciated. I'm pretty certain this can be done, and even extended for hits today, hits this week, hits ever...

------------------
The Immuatable Order of Modding
-=-=-=-=-=-=-=-
1. Read the FAQ, 2. Search the board, 2a. Search the board again, 3. ask the question, 4. back-up, 5. experiment, 6. rephrase question (or better yet, post solution to original question)

[This message has been edited by oldmoney (edited January 21, 2000).]
Quote Reply
Re: View counter on long format record... In reply to
Something that works for me:
Define a field, called 'Teller', than add the following code in your 'html_record_long':
Code:
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{'Teller'};
$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;
Hope this helps.
--------
Mart
Quote Reply
Re: View counter on long format record... In reply to
Looks good... since you're writing to the DB on every view of the long format record, have you noticed a slowdown in access (obviously dependent on number of records and fields)? I'm inclined to use the direct route like you have done... now just need to add the cookie check to stop multiple hits from registering. Should be an easy mod from here... I will post when I have the time.

------------------
The Immuatable Order of Modding
-=-=-=-=-=-=-=-
1. Read the FAQ, 2. Search the board, 2a. Search the board again, 3. ask the question, 4. back-up, 5. experiment, 6. rephrase question (or better yet, post solution to original question)
Quote Reply
Re: View counter on long format record... In reply to
I've made this myself, it creates a temp-file in a temp-directory and write the counter-hits to it, every time, the record is hit.
Code:
$count_up = "/path/to/your/server/dbman/temp/$rec{$db_key}";
open (COUNT, "<$count_up");
$countup = <COUNT>;
close COUNT;
open (COUNT, ">$count_up");
$countup2 = $countup + 1;
print COUNT $countup2;
close COUNT;
Than, every night, a cron updates it:
Code:
sub count_update {
# --------------------------------------------------------
# Updates the database-counter field

my (@data, $output);

open (DB, "<$db_file_name1") or &cgierr("error in modify_records. unable to open db file: $db_file_name1.\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_decode2($line);
if (-e "$TEMP_DIRECTORY/$data[0]") {
open (TEMP, "$TEMP_DIRECTORY/$data[0]");
$countplus = <TEMP>;
close "$TEMP_DIRECTORY/$data[0]";
unlink "$TEMP_DIRECTORY/$data[0]";
$data[25] = $data[25] + $countplus;
$counttotaal = $counttotaal + $countplus;
$output .= join ("|",@data) . "\n";
}
else {
$output .= $line . "\n";
}
}

open (DB, ">$db_file_name1") or &cgierr("error in modify_records. unable to open db file: $db_file_name1.\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;
The only thing, I don't like of this, is that a user can't see directly his number of hits on each moment of a day, he have to wait to the next day, to see how many times his page is clicked the past day.

Maybe this helps,
--------
Mart.

Quote Reply
Re: View counter on long format record... In reply to
Thank you... I'm still trying to decide which is the better of the two approaches you came up with. I appreciate your help.

------------------
The Immuatable Order of Modding
-=-=-=-=-=-=-=-
1. Read the FAQ, 2. Search the board, 2a. Search the board again, 3. ask the question, 4. back-up, 5. experiment, 6. rephrase question (or better yet, post solution to original question)
Quote Reply
Re: View counter on long format record... In reply to
Hello,

I recently implimented mart's second codeset, and found a nice way to use it without having to write to the database file. It's pretty simple, actually.

in sub html_record, place the following code:

Quote:
$count_up= "/path/to/your/server/dbman/temp/$rec{$db_key}";
open (COUNT, "<$count_up");
$countup = <COUNT>;
close COUNT;
open (COUNT, ">$count_up");
$countup2 = $countup + 1;
print COUNT $countup2;
close COUNT;

Then, in sub html_record when you are defining the structure of the page layout, wherever you want to show the number of hits made to the record, simply reference $countup2. The read/write is already done for you, and you don't have to worry about what might happen to your database due to writing to it constantly.

Your dbman/temp directory might wind up with a lot of files in it, but they're all rediculously small. Also, if you ever want to "reset" the counter, simply delete a specific file or all files from the directory.

Just thought I'd share. Thanks for the code, Mart! Smile


----------------------------------------
Lee Leisure
HP Customer Relations Manager
----------------------------------------
* NOTE: This message is intended
for personal purposes only, and does
not imply the position or opinion of
the Hewlett Packard Company.




[This message has been edited by leisurelee (edited March 23, 2000).]
Quote Reply
Re: View counter on long format record... In reply to
Lee, that's the best counter idea I've seen. Very nice.

The other idea I just had would be to keep a file that holds the $db_key values of each record on an individual line. Then, when a record is accessed, it would count the number of times the $db_key value appears in the file. But your solution is much faster and definitely more elegant.


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





Quote Reply
Re: View counter on long format record... In reply to
I am trying to implement this mod and I am having a problem.

I am using the short and long format to display records, if I "list all records" and then go to long long format of a record it does not increment. However, if I list the records by category, or do a search, it increments correctly.

Could this somehow be related to ID=* in the query string? Also, when I go to the long record after a view all, the hit counter is always 1.

Any ideas? Thanks!

Quote Reply
Re: [Morgan] View counter on long format record... In reply to
In Reply To:
I am trying to implement this mod and I am having a problem.

I am using the short and long format to display records, if I "list all records" and then go to long long format of a record it does not increment. However, if I list the records by category, or do a search, it increments correctly.

Could this somehow be related to ID=* in the query string? Also, when I go to the long record after a view all, the hit counter is always 1.

Any ideas? Thanks!


Sorry to bring this up after such a long time but I was wondering if this problem was ever resolved.
I'm having the same problem with the long record MOD. The hit counter is always 1 in the view results page, yet it changes in the delete or modify pages.

Any ideas why is that?
Thanks!
Quote Reply
Re: [tsiftis] View counter on long format record... In reply to
There's a later thread with what I think is the final version of that mod. Check out:

http://www.gossamer-threads.com/perl/gforum/gforum.cgi?do=ubb&ubb=002625:Forum12

and see if that provides your solution.

There are also other versions in the DBMan FAQ.


I have always used this version and have never had a problem. This also gives you the top hits.

Updated MOD: Top 10/Record Hit Counter (More Accurate)
http://www.gossamer-threads.com/perl/gforum/gforum.cgi?post=16414


Hope this helps

Unoffical DBMan FAQ

http://creativecomputingweb.com/dbman/index.shtml/

Last edited by:

LoisC: Dec 19, 2006, 5:57 PM
Quote Reply
Re: [LoisC] View counter on long format record... In reply to
Thanks for the reply Lois.

I already went through the post you provided, and I also browsed extensively in this forum and in your excellent DBman FAQ web site.
However my problem is exactly the same as described above by Morgan.

I'm using the long/short display record and I use the counter in the long display page. When I click on "List all records" the counter shows 1 view. However if I go to the record through a regular search, or when I go to the "Delete record" display page the counter shows the correct number of hits.

As Morgan notices above the problem is with ID=*.
When I remove the * then the record shows the correct number of hits. That works when I login as the author or as a regular user and there are more than 2 records listed in the database.

If I login as admin and I remove the * after the ID then I get the message "no search terms specified".

Any suggestions/ideas on how can this be resolved?
Quote Reply
Re: [tsiftis] View counter on long format record... In reply to
The problem must be with using the long long format and I'm not familiar with using that with a hit counter.

Is your dbkey your record ID number? $db_key = 'ID';

Perhaps Carol (JPDeni) may have some suggestions. Sorry I couldn't be of any help.

Unoffical DBMan FAQ

http://creativecomputingweb.com/dbman/index.shtml/
Quote Reply
Re: [tsiftis] View counter on long format record... In reply to
Your problem is likely due to bolding. Just for a test, turn off bolding in your .cfg file and see if you have the same problem. If you don't, then you'll need to remove bolding tags from the $db_key value before you do the search for the counter.


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