Page 1 of 1

Linking to several variables in one text file - Please help!

Posted: Mon Feb 24, 2003 4:26 pm
by MastahUK
I am currently trying to add a rating system to my site, where people can each vote and place the rating between 1 and 10 using a form. I would like to have a text file (or .dat or whatever) where this value (lets call it $rating) gets sent to. In the same text file, I would also like to have a variable that gets added by one every time somebody votes (called $votes). These two variables will then be used to calculate the overall average ($avg) by dividing the total of the ratings by the number of votes.

Could somebody produce a simple script (comments would be very helpful also) where all of this works. I will need to use it like a cgi script by linking to the code when you click submit on the form that has the rating in it. The variable $rating will need to be added to the current value in the file, it must not overwrite it.

At the moment I do not know how to use several variables in the same file so I attempted using several files but it did not work. Here is what I have to give you a rough idea (the value 'rating' is defined on the page that links to this file):

# Sets filenames for use later.
$rating = $FORM{'rating'};
$url = $FORM{'url'};
$datafolder = '';

# Set the counter file.
$count_file = "../rate/" . "$rating" . ".dat";
$visits = "../rate/" . "$rating" . "1.dat";
$total = "../rate/" . "$rating" . "2.dat";

# Open the counter files and gets current values.
open (COUNT, $count_file);
close (COUNT);
open (COUNT, $visits);
close (COUNT);

# Increase the counters appropriately.
$current_count = $current_count + $rating;

# Open the counter files for replacing current numbers with new ones.
open (COUNT, ">$count_file");
print COUNT $current_count;
close (COUNT);
open (COUNT, ">$visits");
print COUNT $current_visits;
close (COUNT);

# Work out average
$avg = $current_count / $current_visits;
open (COUNT, ">$total");
print COUNT $avg;
close (COUNT)

I will be VERY grateful if somebody could get this to work for me, it has been causing me grief for a few days now. Thankyou.

Posted: Mon Feb 24, 2003 4:31 pm
by MastahUK
Rather than fixing this script, it would probably be easier to start from scratch. There is probably too much wrong with my script to mean it is not worth salvaging. Plus I do not really want to link to 3 files for a single rating anyway.

Reply : question

Posted: Tue Feb 25, 2003 8:01 am
by Skeletor
Do you want the flat file to hold only one number for $rating and $votes? such as:

data file: #??? #???

when someone submits the form, it fetches the number for $rating and $votes from the data file and then adds the 1-10# to $rating and adds 1 to $votes. It will then write the new values to the data file.

I think this is what you want, I just wanted to make sure.

Posted: Wed Feb 26, 2003 4:05 am
by MastahUK
Precisely yes, and it would be great if this could happen in only 1 file rather than 3. If you dont know how to code the rest of it, could you just tell me the code that can load 3 variables from one text file. Of course it would be very helpful with a full code.....:D

Posted: Wed Feb 26, 2003 4:39 am
maby the bast way to download some ready script for voiting from some resource: ... tegory=php