Gossamer Forum
Home : Products : DBMan : Customization :

Ask for adding the "Rate" into dbman-sql version

(Page 1 of 3)
> >
Quote Reply
Ask for adding the "Rate" into dbman-sql version
Hello all,


How can I let the Public user rate the individual record like cgi-resources in DBman-sql version?


Best regards


Gab
Quote Reply
Re: Ask for adding the "Rate" into dbman-sql version In reply to
Look at the rate.cgi file in LINKS as a template for implementing this type of option in DBMAN, then you will have to re-write it to work with the SQL version.

Regards,

------------------
Eliot Lee
Anthro TECH,L.L.C
www.anthrotech.com
* Be sure to visit the Resource Center for FAQ's, Modifications and Extra Goodies!!
* Search Forums!
* Say NO to Duplicate Threads. :)
----------------------








Quote Reply
Re: Ask for adding the "Rate" into dbman-sql version In reply to
Hi Eliot,

How hard is it to implement this code? I have tried unsuccessfully to implement both rate.cgi and the hit counter code ( www.gossamer-threads.com/scripts/forum/resources/Forum12/HTML/001933.html ) in a seperate sub in dbman. I have tried putting the hit counter code in a view_records like sub and taking out the display of the records, so if I did a view_records=1&ID=(id number), it would just increase the counter for that ID by 1. I always get an unknown action error. Please help!

[This message has been edited by blink101 (edited January 29, 2000).]
Quote Reply
Re: Ask for adding the "Rate" into dbman-sql version In reply to
Well, you are getting the unknown action error because you have not defined the sub-routine in the elsif statements in the sub main routine in the db.cgi file.

Also, I would recommend putting the rating outside of the db.cgi as a separate script.

Regards,



------------------
Eliot Lee
Anthro TECH,L.L.C
www.anthrotech.com
* Be sure to visit the Resource Center for FAQ's, Modifications and Extra Goodies!!
* Search Forums!
* Say NO to Duplicate Threads. :)
----------------------








Quote Reply
Re: Ask for adding the "Rate" into dbman-sql version In reply to
Hi Eliot,

Thanks for the tip. I've included my code so maybe you can spot any glaring errors or missing comments. Thanks!

1. Added following in db.cgi:
Code:
elsif ($in{'vote'}) { if ($per_view) { &html_vote; } }
2. Added "Teller" field in default.cfg, as numer, with default of 0.
3. Added new sub to html.pl, below:
Code:
sub html_vote {
#----------------------------------------------------------------
my (%rec) = @_;

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;
&html_print_headers;
# print out the links
print qq|
<html>
<head>
<title>$html_title: Vote Sucessful.</title>
</head>
<body>
<$font>Your vote was recorded!</font>
</body>
</html>
|;

}

4. Thought that this would add 1 to the Teller field (of record 409 when I called:
[mysite]/cgi-bin/test/db.cgi?uid=default&vote=1&ID=409

Instead it just prints out the message Frown Any suggestions? Thanks as always!

Cary

[This message has been edited by blink101 (edited January 29, 2000).]
Quote Reply
Re: Ask for adding the "Rate" into dbman-sql version In reply to
1) Create a new sub-routine for the voting form.

2) Then use the "vote" sub-routine for confirmation that the vote has been submitted.

You have to create a form routine that adds the vote into the "Teller" field. The codes you are using are for the Counter Mod, which adds hit counter to the record upon reloading the window.

Nice attempt though...You are getting there.

Regards,

------------------
Eliot Lee
Anthro TECH,L.L.C
www.anthrotech.com
* Be sure to visit the Resource Center for FAQ's, Modifications and Extra Goodies!!
* Search Forums!
* Say NO to Duplicate Threads. :)
----------------------








Quote Reply
Re: Ask for adding the "Rate" into dbman-sql version In reply to
Hi Eliot,

Okay, I'm getting warm...I think. I've added a new sub:
Code:
sub html_vote_record {

my (%rec) = @_;

&html_print_headers;
print qq|
<html><head><title>$html_title: Vote Record.</title></head>
<body link="#808080" vlink="#808080" alink="#808080" bgcolor="#FFFFFF">
<form action="$db_script_url" method="POST">
<input type=hidden name="db" value="$db_setup">
<input type=hidden name="uid" value="$db_uid">
<input type=hidden name="Teller" value="$rec{'Teller'}">
<p><font face="arial"><b>Vote</b></font><br>
<$font>
|;
print qq|
</font></p>
<p><INPUT TYPE="SUBMIT" NAME="vote" VALUE="Vote">
<INPUT TYPE="RESET" VALUE="Reset Form"></p></form><P>|;

&html_footer;
print qq|</body></html>|;
}

And the Vote sub changed to:
Code:
sub html_vote {
my (%rec) = @_;
$rec{'Teller'} + 1;
&html_print_headers;
# print out the links
print qq|
<html>
<head>
<title>$html_title: Vote Sucessful.</title>
</head>
<body>
<$font>Your vote was recorded!</font>
</body>
</html>
|;

}
Now, from what you said, it looks like the code to increment Teller could be in the wrong sub. And is this even the correct code to increment by 1? I tried this, and it would show the vote button page, and upon clicking, it shows confirmation - but still doesn't imcrement. Hopefully you can see what's wrong so I can finally get this thing working! Thanks Smile

Cary
Quote Reply
Re: Ask for adding the "Rate" into dbman-sql version In reply to
I would go about this differently. I would actually allow people to vote from 1-10 for the resource.

Regards,

------------------
Eliot Lee
Anthro TECH,L.L.C
www.anthrotech.com
* Be sure to visit the Resource Center for FAQ's, Modifications and Extra Goodies!!
* Search Forums!
* Say NO to Duplicate Threads. :)
----------------------








Quote Reply
Re: Ask for adding the "Rate" into dbman-sql version In reply to
Hi Eliot,

The thing is that the votes would be counted for people, not resources. Either way, can you figure out what I'm doing wrong/suggest code fixes? Just whatever (increment by 1 or 1-10) would be easier. I know that rate.cgi should be helpful, but the processes in it look Greek to me Frown Thanks.

Cary
Quote Reply
Re: Ask for adding the "Rate" into dbman-sql version In reply to
And people, I assume, are records in your database, right? If not, explain further exactly what you are trying to accomplish.

Regards,

------------------
Eliot Lee
Anthro TECH,L.L.C
www.anthrotech.com
* Be sure to visit the Resource Center for FAQ's, Modifications and Extra Goodies!!
* Search Forums!
* Say NO to Duplicate Threads. :)
----------------------








Quote Reply
Re: Ask for adding the "Rate" into dbman-sql version In reply to
Correct. People have their information stored in each record. Someone goes through the records, and votes for people they like. The vote either increases the votes for that person by one, or like you suggested, they rate the person from 1-10. I didn't think I had far to go from my last code post, but I still haven't figured out how to get the subs to increment Teller by 1 (or which sub should perform this). Thanks!

Cary
Quote Reply
Re: Ask for adding the "Rate" into dbman-sql version In reply to
Hint: The incrementation should occur separately in the db.cgi file. The two sub-routines you have are correct in terms of providing a form and confirmation screen.

Regards,




------------------
Eliot Lee
Anthro TECH,L.L.C
www.anthrotech.com
* Be sure to visit the Resource Center for FAQ's, Modifications and Extra Goodies!!
* Search Forums!
* Say NO to Duplicate Threads. :)
----------------------








Quote Reply
Re: Ask for adding the "Rate" into dbman-sql version In reply to
Hi Eliot,

Can you please let me know how I can do this incrementation in db.cgi? I have no idea what to do!!! Thanks.

Cary
Quote Reply
Re: Ask for adding the "Rate" into dbman-sql version In reply to
Okay...give this a shot....

1) Add third sub-routine, but in your db.cgi, called sub add_votes. The sub-routine should look something like this:

Code:
sub add_votes {
#-------------------------------------------
# Add votes to Rating Field

my $id = $in{'db_key'}
my $vote = $in{'vote'}
my $time = time();

# Check for proper rating

unless (($vote =~ /^\d\d?/) and ($vote >= 1) and ($vote <= 10)) {
&html_vote_failure ("Your rating '$rating' is invalid.");
return;
}

# Let's get the link information.

my %rec = &get_record;
($rec{$db_key} eq $id) or (&html_vote_failure ("Unable to find link with ID '$id'.") and return);

# Increase the vote.

if (open (HIT, "<$db_rates_path/$id")) {
my $input = <HIT>; chomp $input;
($votes, $old_rating) = split /\s/, $input;
chomp ($old_time = <HIT> );
chomp (@IP = <HIT> );
(($time - $old_time) > 172800) and (@IP = ());
foreach $ip (@IP) {
$ip eq $ENV{'REMOTE_ADDR'} and ($visited++ and last);
}
close HIT;
if (!$visited) {
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}) {
$votes = $votes + 1;
$rec{'Teller'} = $rating + $old_rating;
$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;
&html_vote_success;
}
else {
&html_vote_failure ("Sorry, you've already rated this resource once recently.");
}
}

2) Create another sub-routine in your html.pl called sub html_vote_failure. This routine should look similar to your sub html_add_failure.

3) Create another directory variable in your default.cfg file called:

Code:
$db_rates_path

This can be put in your DBMAN directory as a sub-directory. Make sure that you change the permission of this directory to 777.

4) Change the following codes in your sub html_vote_form:

Code:
<INPUT TYPE="SUBMIT" NAME="vote" VALUE="Vote">

to the following:

Code:
<INPUT TYPE="SUBMIT" NAME="add_votes" VALUE="Vote">

5) Rename your sub html_vote in the html.pl file to sub html_vote_success.

6) Change the following codes:

Code:
<input type=hidden name="Teller" value="$rec{'Teller'}">

to the following:

Code:
Rate this Record:
<select name="Teller">
<option value="---"></option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>

See if this works!

Regards,

------------------
Eliot Lee
Anthro TECH,L.L.C
www.anthrotech.com
* Be sure to visit the Resource Center for FAQ's, Modifications and Extra Goodies!!
* Search Forums!
* Say NO to Duplicate Threads. Smile
----------------------










[This message has been edited by Eliot (edited January 30, 2000).]

[This message has been edited by Eliot (edited January 30, 2000).]
Quote Reply
Re: Ask for adding the "Rate" into dbman-sql version In reply to
Hi Eliot,

I'm getting an Internal Server Error. What extra fields should I have? Just Teller, or Teller and vote? Also, what elsif's should I have in db.cgi? The 500 error is coming as soon as I call a sub. Thanks!
Quote Reply
Re: Ask for adding the "Rate" into dbman-sql version In reply to
Teller...If you notice, I defined $vote locally via the my operator!

Oops...saw one error in the codes I posted:

Change the following:

Code:
$votes = $votes + 1;

to the following:

Code:
$vote = $vote + 1;

Use telnet to check the syntax of the file to see EXACTLY what the syntax error is!

Don't know how to do this????

1) Connect to your DBMAN CGI-BIN directory:

Code:
cd /cgi-bin/dbman/

2) Check syntax

Code:
perl -c db.cgi

Also, look at your Perl Error Log...Don't know how to do this? Contact your hosting company.

Want some other commands? Get a UNIX manual!

Regards,

------------------
Eliot Lee
Anthro TECH,L.L.C
www.anthrotech.com
* Be sure to visit the Resource Center for FAQ's, Modifications and Extra Goodies!!
* Search Forums!
* Say NO to Duplicate Threads. Smile
----------------------










[This message has been edited by Eliot (edited January 30, 2000).]
Quote Reply
Re: Ask for adding the "Rate" into dbman-sql version In reply to
Hi Eliot,

Okay, I think we're getting close here. I put ; after "my $id = $in{'db_key'}" and "my $vote = $in{'vote'}" and added a closing right bracket to the end of the sub...the db.cgi now runs fine. However, after I vote, I get a "Unable to Add Record - There were problems with the following fields: Your rating '' is invalid." message. Any ideas? Thanks for everything Smile
Quote Reply
Re: Ask for adding the "Rate" into dbman-sql version In reply to
Did you change the sub html_vote_form to include the select drop-down menu for the Teller field like I posted??

Did you actually choose a number? If you simply click on the submit button without selecting a number then you will get that error....

Look at the following lines:

Code:
unless (($vote =~ /^\d\d?/) and ($vote >= 1) and ($vote <= 10)) {
&html_vote_failure ("Your rating '$rating' is invalid.");
return;
}

Translation: If you do not choose a number equal to or greater than 1 or less than or equal to 10, then you will get that error message.

BTW: I noticed that you should also change the following codes:

Code:
&html_vote_failure ("Your rating '$rating' is invalid.");

to the following codes:

Code:
&html_vote_failure ("Your rating '$vote' is invalid.");

Regards,

------------------
Eliot Lee
Anthro TECH,L.L.C
www.anthrotech.com
* Be sure to visit the Resource Center for FAQ's, Modifications and Extra Goodies!!
* Search Forums!
* Say NO to Duplicate Threads. :)
----------------------








Quote Reply
Re: Ask for adding the "Rate" into dbman-sql version In reply to
Hi Eliot,

Yes, I actually entered a number to vote. My html_vote_form is below:

Code:
sub html_vote_form {
my (%rec) = @_;
&html_print_headers;
print qq|
<html><head><title>$html_title: Vote Record.</title></head>
<body link="#808080" vlink="#808080" alink="#808080" bgcolor="#FFFFFF">
<form action="$db_script_url" method="POST">
<input type=hidden name="db" value="$db_setup">
<input type=hidden name="uid" value="$db_uid">
<p><font face="arial"><b>Vote</b></font><br>
<$font>Rate this Record:</font>
<select name="Teller"> <option value="---"></option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>

|;
print qq|

<p><INPUT TYPE="SUBMIT" NAME="add_votes" VALUE="Vote"> <INPUT TYPE="RESET" VALUE="Reset Form"></p></form><P>|;
&html_footer;
print qq|</body></html>|;
}

Thanks,
Cary
Quote Reply
Re: Ask for adding the "Rate" into dbman-sql version In reply to
Oops...I am so sorry, you need to have the variable in the votes in your sub html_vote_form, like the following:

Code:
sub html_vote_form {
my (%rec) = @_;
&html_print_headers;
print qq|
<html><head><title>$html_title: Vote Record.</title></head>
<body link="#808080" vlink="#808080" alink="#808080" bgcolor="#FFFFFF">
<form action="$db_script_url" method="POST">
<input type=hidden name="db" value="$db_setup">
<input type=hidden name="uid" value="$db_uid">
<p><font face="arial"><b>Vote</b></font><br>
<$font>Rate this Record:</font>
<select name="vote"> <option value="---"></option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
|;
print qq|
<p><INPUT TYPE="SUBMIT" NAME="add_votes" VALUE="Vote"> <INPUT TYPE="RESET" VALUE="Reset Form"></p></form><P>|;
&html_footer;
print qq|</body></html>|;
}

I am really sorry about this. I misunderstood your earlier question. Yes it should be votes for the select field.

Regards,

------------------
Eliot Lee
Anthro TECH,L.L.C
www.anthrotech.com
* Be sure to visit the Resource Center for FAQ's, Modifications and Extra Goodies!!
* Search Forums!
* Say NO to Duplicate Threads. :)
----------------------








Quote Reply
Re: Ask for adding the "Rate" into dbman-sql version In reply to
Hey Eliot,

No problem at all - without you I wouldn't even be close to getting this to work Smile Now, I still have a little problem (sorry to keep on bugging you!)...can you explain what Teller is? Is it just the new rating (i.e. 8.4)? I get the html_vote_success page, but Teller is unchanged in the record. Is there supposed to be anything in the rates folder? I set up the the path to be:
Code:
$db_rates_path = $db_script_path . "/rates";
in default.cfg. Thanks.

Cary
Quote Reply
Re: Ask for adding the "Rate" into dbman-sql version In reply to
Oopss...I guess that step was not clear enough.

You have the correct variable setting.

Yes...You will need to create a /rates/ sub-directory below your dbman directory.

About the Teller...It is the field in your database. So, you should have added this field to your %db_def section and re-built your MySQL table to include this field in the table.

Since you said "8.4", I am assuming that some number has been added to that field in the record you voted, right?

BTW: I added codes that prohibits people to vote for the same record within a certain period of time, which I set to 172800, which is 2 days....(That is what the /rates/ directory is for and the id files that should be built in there. Unfortunately, I do not know of a way to delete these files in DBMAN other than setting a crontab to rm these files every few hours or use the del command and set this to a certain time in the sub add_votes, which is complicated to do, and also uses a lot of server resources.)

Feel free to change this to something like 21600, which is 6 hours.

Also, make sure that you change all incidences of $votes to $vote in the sub add_votes routine in db.cgi.

Regards,

Regards,

------------------
Eliot Lee
Anthro TECH,L.L.C
www.anthrotech.com
* Be sure to visit the Resource Center for FAQ's, Modifications and Extra Goodies!!
* Search Forums!
* Say NO to Duplicate Threads. Smile
----------------------










[This message has been edited by Eliot (edited January 31, 2000).]
Quote Reply
Re: Ask for adding the "Rate" into dbman-sql version In reply to
Hmm,

This is strange - I did add Teller as a new field, changed my default.db to reflect it, made the rates sub-directory, and chmoded it to 777. Also, all $votes were changed to $vote. Yet, I always get the success page even though no records are updated (I've also checked to make sure I was calling the correct IDs for testing).
Quote Reply
Re: Ask for adding the "Rate" into dbman-sql version In reply to
You are using SQL, right? You have to add this field into your existing MySQL table!

Regards,

------------------
Eliot Lee
Anthro TECH,L.L.C
www.anthrotech.com
* Be sure to visit the Resource Center for FAQ's, Modifications and Extra Goodies!!
* Search Forums!
* Say NO to Duplicate Threads. :)
----------------------








Quote Reply
Re: Ask for adding the "Rate" into dbman-sql version In reply to
Hi Eliot,

Nope, the other guy is using a SQL database. I'm using the regular flat-file.

Cary
> >