...

View Full Version : Item rating system with flat files



T.K.
03-02-2009, 04:50 PM
Hi,

I'm wondering how to create a flat file based item rating system that has ratings from 1 point to 5 points. Finally, the script would echo the average rating of the item and displays the amount of votes. It also allows to cast one vote per ip address. I don't have a clue how to do this, so any help concerning the issue would be appreciated.

Here's my start:

ITEM (e.g. picture)



<form method="post" action="rate.php">
<select name="rating">
<option>1 point - Terrible</option>
<option>2 points - Bad</option>
<option>3 points - Fair</option>
<option>4 points - Good</option>
<option>5 points - Excellent</option>
</select>
<input type="submit" value="Rate!" />
</form>

Fumigator
03-02-2009, 06:46 PM
Any compelling reason you won't be able to use a relational database (MySQL for example)?

T.K.
03-02-2009, 08:49 PM
My web hotel service provider don't offer a relational database as a free attribute.

Fumigator
03-02-2009, 10:23 PM
Alright well it's all certainly very possible to do this using flat files, but it will be kludgy. You'll need to become familiar with how to open, read, write, append and rewrite flat files.

http://us2.php.net/manual/en/ref.filesystem.php

The general design will consist of functions to: 1. add a new rating (open and append), and 2. display a rating (open and read).

The unique IP address portion is tough to do without a login system and should be a separate discussion (search this forum, it's been discussed many times before).

T.K.
03-03-2009, 05:27 PM
Ok, this is what I came up with:



<html>
<head>

<title>Rating script</title>


</head>


<body>

<p>Rate this paragraph!</p>

<form method="post" action="rate_index.php">
<select name="rating">
<option>Terrible</option>
<option>Bad</option>
<option>Fair</option>
<option>Good</option>
<option>Excellent</option>
<input type="submit" name="submit" value="Rate!" />
</form>

<?php

$Rates = $_POST["rating"];

$File1 = "rate_data.php";
$Handle = fopen($File1, 'a+');
fwrite($Handle, $Rates."\n");
fclose($Handle);

$File2 = "rate_data.php";
$Lines = file($File2);
$Count = count($Lines);
echo "Total rates: ".$Count.".<br/><br/>";

?>

</body>
</html>


The code above writes a selected rating into a remote file and counts the number of total ratings. My solution may be rough, but I'm just a newbie with programming.

Now I ought to know how to count the rating which has most votes and finally to display it. I've already spent several hours googlin' around to find out how to implement that. Any guide to right direction would be more than helpful :)

Fumigator
03-03-2009, 05:58 PM
You are in luck, because there is a PHP function to do just that. It's called array_count_values().

http://us3.php.net/manual/en/function.array-count-values.php

It will produce an array that uses your ratings as the index names and the number of times they occur as the values. You then just need to sort that array descending to pop the most common rating to the top.

http://us3.php.net/manual/en/function.rsort.php

T.K.
03-03-2009, 06:12 PM
Thanks! I just found the same function by myself, heh.

T.K.
03-06-2009, 04:39 PM
Well, I searched the forum and found nothing really useful than the information that I should avoid using cookies in preventing a user submitting the form twice (in case the user deletes them). So, maybe I ought to try with ip. I do know how to get user's ip address but need to be aware how to insert it into an array and then execute a query (maybe using "in_array" function) if the ip already exists in the array and then disallow resubmission.

Here's the current code I'm using:



THE HTML FORM:

<form method="post" action="rate.php" name="form1">
<legend>RATE THIS ITEM</legend>
<select name="rating">
<option>Terrible</option>
<option>Bad</option>
<option>Fair</option>
<option>Good</option>
<option>Excellent</option>

<input type="submit" name="submit" value="Rate!" />
</form>

And the PHP:

<?php

$File1 = file_get_contents("rate_data.php");
$Words = (array_count_values(str_word_count(strtolower($File1),1)));
arsort($Words);

if($File1 == NULL) {
echo "<p>Currently no ratings.</p>";
}
else {
echo "<p><b>Rated:</b> ".key($Words)."</p>";
}

echo "<p><b>Vote statistics:</b></p>";
if($File1 == NULL) {
echo "<p>No stats available.</p>";
}
else {
$File3 = file_get_contents("rate_data.php");
$Words = (array_count_values(str_word_count(strtolower($File3),1)));
arsort($Words);
foreach ($Words as $key => $val) {
echo "<p>$key = $val votes.</p>\n";
}
}

$File2 = "rate_data.php";
$Lines = file($File2);
$Count = count($Lines);
echo "<p><b>Total rates:</b> ".$Count.".</p>";

?>


Here's the source code of the rate.php:



<?php

$URL="rate_index.php";

header ("Location: $URL");

$Rates = $_REQUEST["rating"];
$IP = $_SERVER["REMOTE_ADDR"];

$File1 = "rate_data.php";
$Handle = fopen($File1, 'a+');
fwrite($Handle, $Rates."\n");
fclose($Handle);

?>


Thanks in advance for your help.

Fumigator
03-06-2009, 04:50 PM
You can see the IP address in the global variable $_SERVER['REMOTE_ADDR']. This value comes through the header and can easily be spoofed by the technically savvy ones. The level of spoofage will be directly related to your user's demographics; are they computer geeks, or grandmas? How much advantage will they gain by cheating the rating system? Is there money or status to be gained by cheating the ratings? These factors will mold your system.

T.K.
03-07-2009, 11:48 AM
I changed the script that it will ask a reviewer's email address and then compares it to the the data file if it already exists. So everyone can rate only once with a valid email. Of course it doesn't stop people to do multiple rates with different email addresses, but as the script is for people to rate different lures (the website is a fishing portal), not a "bigger" question, I hardly think that many people would bother to register (if doesn't already possess) several email accounts to rate more than once. However, any suggestions how to make it more reliable (for someone would yet cheat the system)?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum