Gossamer Forum
Home : Products : DBMan : Customization :

Top ten hits record

(Page 1 of 2)
> >
Quote Reply
Top ten hits record
hi everyone,
I would like to ask how to do the top ten hit record ?? i read the following threadshttp://www.gossamer-threads.com/scripts/forum/resources/Forum12/HTML/001933.html
it provides me lot of useful information regarding to how to make a hit counter for each record. I think the best sugguestion is from leisurelee. We just need to add a few line into the html.pl to get it to work.Cool .. Smile
I am wondering if it is possible to find out the Top ten hits counter in the DB??
Thanks
Quote Reply
Re: Top ten hits record In reply to
You would have to implement the counter first. As you saw, there were several different ideas about how to implement the counter -- write to the database with each record, keep a file for each record with the number of hits, or keep one "hits" file with the $db_key values in it. You would need to decide which one to use and test it out before thinking about how to do a "top ten."

Any computation of a "top ten" would be dependent upon the way the record hits were recorded.


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






Quote Reply
Re: Top ten hits record In reply to
  Actually i am using the hit counter sugguested by leisurelee. It works very well. So,is it possible to make the Top ten hit conunter ??
Thanks
Quote Reply
Re: Top ten hits record In reply to
This would be noteably tricky, as I don't believe you can sort by the hit counter unless it is a database record. With my variant, you're writing a file for every record that contains the count number. I'm sure it's possible to derive the top 10 from those files, but I, alas, don't exactly know how to do that. Hopefully another codewiz here has some ideas?

If I think of anything, though, I will post it. Smile

--Lee
Quote Reply
Re: Top ten hits record In reply to
hi leisurelee,
Thanks a lot.
Any other sugguestion would be appreicated
Does any body know how to do???
Quote Reply
Re: Top ten hits record In reply to
I'll have to think about how to code this, but the procedure would be as follows:

Open every file in the directory -- do the counter files have anything in common? I'll have to look at Lee's code, but it seems that the filenames are just the key value of the record, with no extension. If so, that will make it easier to figure out which files you want to use.

Create a hash in the form of

$hash{$filename} = file contents

Sort the hash by the value of each entry, in descending order and print out the first 10.

Of course, printing out the records will take a little more work because all you have is the key value and you probably would want something from the record to use as a link. So you would have to call &get_record for each one to get some information.

I will have to think about how to code what I wrote above. This isn't a real easy thing to do.


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






Quote Reply
Re: Top ten hits record In reply to
JPD,

Yes, the code used writes a single file for each record, named for its db ref number. The contents of the file are the number of hits, nothing else. If there are no hits, there is no file. Smile

So, record 1, the hit count file is something like /httpd/cgi-bin/dbman/temp/1 and in the file is the number of hits.

--Lee
Quote Reply
Re: Top ten hits record In reply to
That is very elegant, Lee. I like it a lot. Smile

So it goes into a subdirectory of the dbman directory? That makes things a little easier. (I ought to have looked more closely at what you wrote in the other thread. Smile )

I'll play with this a bit and see what I can come up with.




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






Quote Reply
Re: Top ten hits record In reply to
Okay. I think I got it.

Add the following to wherever you want your top ten list to print out -- probably in sub html_home:

Code:
$temp_dir = 'path/to/temp/directory; # Note there is no trailing slash!
opendir (TEMPDIR, "$temp_dir") or &cgierr("unable to open directory $temp_dir. Reason: $!");
@files = readdir(TEMPDIR); # Read in list of files in directory..
closedir (TEMPDIR);
FILE: foreach $file (@files) {
next if ($file =~ /^\./); # Skip "." and ".." entries..
next if ($file =~ /^index/); # Skip index.htm type files..
open (COUNTER, "<$temp_dir/$file") or &cgierr("unable to open file $temp_dir/$file. Reason: $!");
$count{$file} = int(<COUNTER> );
close COUNTER;
}
foreach $key (sort {$count{$b} <=> $count{$a} } keys %count) {
push (@top_ten,$key);
}
for ($i=0;$i<10 ;$i++) {
%rec = &get_record($top_ten[$i]);
if (%rec) {
&html_record(%rec);
}
}

I wasn't sure what you wanted to do with the top ten list after it was generated. The last three lines of code will print out the records in the format defined in sub html_record, in descending order.

You'll also need to delete (or comment out) the following line in sub modify_record in the db.cgi script:

Code:
($restricted = 1) if ($auth_modify_own and !$per_admin);

I didn't test this in DBMan, but I did test it using files on my computer. Seems like it should work.


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








[This message has been edited by JPDeni (edited April 11, 2000).]
Quote Reply
Re: Top ten hits record In reply to
Thanks a lot JPDeni,
i tried many time but its still not working..
The following code:
$temp_dir = 'path/to/temp/directory;
The above path I pointed to where i pointed the hits counter




Quote Reply
Re: Top ten hits record In reply to
i got it..
Change the following code :
Code:
$temp_dir = 'path/to/temp/directory; # Note there is no trailing slash!

With:
Code:

$temp_dir = "path/to/temp/directory"; # Note there is no trailing slash!

Smile

Quote Reply
Re: Top ten hits record In reply to
hhmm....its not really working at all

1.There is only one record shown up, not "top ten records"..

2.I use the short/long display Mod...so when the "top ten record shown up", there is a error message when go to the long display from short display record

The following error message :
Check that they exist, permissions are set correctly and that they compile.
Reason: Can't locate default.cfg in @INC (@INC contains: . /usr/lib/perl5/i386-linux/5.00404 /usr/lib/perl5 /usr/lib/perl5/site_perl/i386-linux /usr/lib/perl5/site_perl .) at db.cgi line 51.

Any suggestion??
Thanks



[This message has been edited by johnsonny (edited April 11, 2000).]
Quote Reply
Re: Top ten hits record In reply to
I was definitely premature in creating a mod for this. Smile

I'm rather confused about this problem. I was only able to test that it would print out the names of the files in order when I tested it on my computer, but I don't know why it wouldn't work with the get_record.

If you're willing to try a little more debugging for me, try this:

Code:
for ($i=0;$i<10 ;$i++) {
print "$top_ten[$i]<BR>";
%rec = &get_record($top_ten[$i]);
if (%rec) {
print qq|
<a href="$db_script_link_url&$db_key=$rec{$db_key}&mh=1&view_records=1">$rec{'FieldName'}</a><BR>|;
}
}

That should print the key value and then a link for each of the top ten. Or at least it will tell me which one of the top ten is printing out -- the first, the last, or somewhere in the middle.


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






Quote Reply
Re: Top ten hits record In reply to
Sorry. I left out a #

You also need one in front of

Code:
<a href="$db_script_link_url&$db_key=$rec{$db_key}&mh=1&view_records=1">$rec{'userid'}</a>|;


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






Quote Reply
Re: Top ten hits record In reply to
Thanks a lot JPDeni,
Dont say "sorry" You do really help me a lot. Without your help,i would have given up the DBman already Smile
It works like a charm now!!
I would like to add one more thing :P
how could i make a clickable link on the top ten record?? so user can click on it to see the long record of the user

Thanks in advance


Quote Reply
Re: Top ten hits record In reply to
Hi JPD. Smile

Ingenious work you've done! On a side note, if one wanted to put a link on the sub html_footer (or even from an external source), what code would they use to link to the "top 10" page? I imagine you would have to put this code into its own sub top_ten and call it?

Merci infiniment de toute votre aide merveilleuse de code!

--Lee
Quote Reply
Re: Top ten hits record In reply to
Ah nevermind, figured that part out.

But, I believe I'm having the same troubles johnsonny was: it only displays one record.

If I use print "$top_ten[$i]<BR>" then it displays all ten ID's of the top ten records.

However, if I replace that with &html_record(&get_record($top_ten[$i])); then only the very first record is displayed.

Any ideas why this is happening?

--Lee

[This message has been edited by leisurelee (edited April 12, 2000).]
Quote Reply
Re: Top ten hits record In reply to
How many files are currently in your temp directory?

The problem you're having with the url has to do with the fact that you didn't do a search to come up with the link. I didn't think about that.

Instead of

Code:
&html_record(%rec);

you're going to have to build the links with the code. Something like:

Code:
print qq|
<a href="$db_script_link_url&$db_key=$rec{$db_key}&mh=1&view_records=1">$rec{'FieldName'}</a>



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






Quote Reply
Re: Top ten hits record In reply to
Thanks JPDeni,
But there is still only one record shown up.
There are 6 records in the temp directory.
Does it work only when the temp directory have more than 10 record??

Thanks


[This message has been edited by johnsonny (edited April 12, 2000).]
Quote Reply
Re: Top ten hits record In reply to
No. It should work when there are less than 10. It's just that I wanted to be sure there was more than 1. Smile

Let me see what I can work out on my computer if there are 6 files.

Works fine on this end.

Let's do some debugging. Change the last bit of code from:

Code:
for ($i=0;$i<10 ;$i++) {
%rec = &get_record($top_ten[$i]);
if (%rec) {
print qq|
<a href="$db_script_link_url&$db_key=$rec{$db_key}&mh=1&view_records=1">$rec{'FieldName'}</a>|;
}
}

to

Code:
for ($i=0;$i<10 ;$i++) {
# %rec = &get_record($top_ten[$i]);
# if (%rec) {
# print qq|
<a href="$db_script_link_url&$db_key=$rec{$db_key}&mh=1&view_records=1">$rec{'FieldName'}</a>|;
# }
print "$top_ten[$i]<BR>";
}

See if more than one thing prints out. You won't get links -- just a list of the keys for the records.


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






Quote Reply
Re: Top ten hits record In reply to
its still not working and got "CGI ERROR" message.

The following is what i put in the html.pl

Code:
$temp_dir = '/home/username/cgi-bin/temp'; # Note there is no trailing slash!
opendir (TEMPDIR, "$temp_dir") or &cgierr("unable to open directory $temp_dir. Reason: $!");
@files = readdir(TEMPDIR); # Read in list of files in directory..
closedir (TEMPDIR);FILE: foreach $file (@files) {
next if ($file =~ /^\./); # Skip "." and ".." entries..
next if ($file =~ /^index/); # Skip index.htm type files..
open (COUNTER, "<$temp_dir/$file") or &cgierr("unable to open file $temp_dir/$file. Reason: $!");
$count{$file} = int(<COUNTER> );
close COUNTER;
}
foreach $key (sort {$count{$b} <=> $count{$a} } keys %count) {
push (@top_ten,$key);
}
for ($i=0;$i<10 ;$i++) {
# %rec = &get_record($top_ten[$i]);
# if (%rec) {
# print qq|
<a href="$db_script_link_url&$db_key=$rec{$db_key}&mh=1&view_records=1">$rec{'userid'}</a>|;
# }
print "$top_ten[$i]<BR>";
}
Could you please point me to the right direction???
Thanks your help again,JPDeni


[This message has been edited by johnsonny (edited April 12, 2000).]
Quote Reply
Re: Top ten hits record In reply to
It didn't like that either. Wink

The actual top 10 records are:
Quote:
127
125
27
13
69
186
18
124
1
14

This is derived properly if I use this code:

Quote:
for ($i=0;$i<10 ;$i++) {
print "$top_ten[$i]<BR>";
}

However, when I include the rest of your code line, it shows me:

Quote:
127
<A HREF=http://15.39.178.51/cgi-bin/list/db.cgi?db=default&uid=default&ID=127&mh=1&view_records=1>127</A>

It does not, however, continue on with any of the other numbers. Frown

So it's parsing once, but it doesn't loop, like it's supposed to.

--Lee
Quote Reply
Re: Top ten hits record In reply to
It's doing the first one, but not the others. Maybe &get_record is in the way.

How about this:

Code:
for ($i=0;$i<10 ;$i++) {
print "$top_ten[$i]<BR>";
$in{$db_key} = $top_ten[$i]
my($status,@hits) = &query{'view'};
%rec = &array_to_hash(0, @hits);
if ($status eq 'ok') {
print qq|
<a href="$db_script_link_url&$db_key=$rec{$db_key}&mh=1&view_records=1">$rec{'FieldName'}</a><BR>|;
}
}

(BTW, I don't know French, but I think I was able to figure out what you wrote before. De nada! Smile )


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






Quote Reply
Re: Top ten hits record In reply to
I fear this didn't work. There appears to be a problem with my($status,@hits) = &query{'view'};. It simply hates that line. I received a couple CGI syntax errors. (change &query to $query, but that still didn't make the code work).

Even if $status is manually set to 'ok', the record info is ( naturally) not passed to the link code.

It does, however, continue to print the top 10 numbers properly. Just doesn't print the links.
Quote Reply
Re: Top ten hits record In reply to
Will I ever get my fingers to put in the right type of brackets? Probably not.

Code:
my ($status,@hits) = &query("view");

However, I don't know if that will work, either.

Another idea. But this won't list the records in the correct order. Still it's a possibilty.


Code:
for ($i=0;$i<10 ;$i++) {
print "$top_ten[$i]<BR>";
push(@search_list,$top_ten[$i]);
}
undef %in;
$in{$db_key} = join '|',@searchlist;
$in('re'} = 1;
$in{'ww'} = 1;
my($status,@hits) = &query('view');
if ($status eq 'ok') {
my ($numhits) = ($#hits+1) / ($#db_cols+1);
for (0 .. $numhits - 1) {
$rec=&array_to_hash($_, @hits);
print qq|
<a href="$db_script_link_url&$db_key=$rec{$db_key}&mh=1&view_records=1">$rec{'FieldName'}</a><BR>|;
}
}

This should (emphasis on "should") create a regular expression in the form of

127|125|27|13|69|186|18|124|1|14

and do one search for all the records at once. The trouble is, it will print out in the order the records are in the database, not in the top ten order.


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






> >