Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

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

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

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

Postby MastahUK » Mon Feb 24, 2003 4:26 pm

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 = 'http://www.mastahuk.com/rate/';

# 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);
$current_rating=<COUNT>;
close (COUNT);
open (COUNT, $visits);
$current_visits=<COUNT>;
close (COUNT);

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

# 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.
MastahUK
New php-forum User
New php-forum User
 
Posts: 6
Joined: Thu Feb 20, 2003 2:10 pm
Location: UK

Postby MastahUK » Mon Feb 24, 2003 4:31 pm

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.
MastahUK
New php-forum User
New php-forum User
 
Posts: 6
Joined: Thu Feb 20, 2003 2:10 pm
Location: UK

Reply : question

Postby Skeletor » Tue Feb 25, 2003 8:01 am

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.
Skeletor
New php-forum User
New php-forum User
 
Posts: 147
Joined: Tue Nov 19, 2002 7:47 am
Location: US, Ohio

Postby MastahUK » Wed Feb 26, 2003 4:05 am

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
MastahUK
New php-forum User
New php-forum User
 
Posts: 6
Joined: Thu Feb 20, 2003 2:10 pm
Location: UK

Postby WiZARD » Wed Feb 26, 2003 4:39 am

maby the bast way to download some ready script for voiting from some resource: http://www.hotscripts.com/search/?query ... tegory=php
User avatar
WiZARD
Moderator
Moderator
 
Posts: 1257
Joined: Thu Jun 20, 2002 10:14 pm
Location: Ukraine, Crimea, Simferopol


Return to PHP coding => General

Who is online

Users browsing this forum: Google [Bot] and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.