View Full Version : Rating system

02-17-2008, 07:54 PM
Hi, I am having soo much trouble trying to find a simple rating system tutorial or script.

Can anyone help me out here?

I want the data to be stored in a mysql database, in a row called score.
When a user clicks an option (1 to 5) it gets the number from the database (say 4.24) then does some maths and puts the data back in (say 4.57) depending on what number they clicked and how many votes there are.

If possible:
Ajax (no reloading the page)
0.00 (2 decimal places)

Thanks for you help

02-17-2008, 11:28 PM
Check out http://www.dhtmlgoodies.com/index.html?whichScript=ajax-poller

Looks pretty nice, just as you asked I believe. See it live at outer reaches (http://outer.reaches.dyndns.org/poller/ajax-poller.php) (for a little while)

02-18-2008, 12:24 AM
Thanks but I cannot get the script to work...

02-18-2008, 12:45 AM
lol we have another thread already happening to sort that one out. Basically, edit your .php files to have the correct MySQL login name, password and database. Then run the table install php file. Rename their .html example they give you to .php and it should work just fine. Here's the other post: http://www.codingforums.com/showthread.php?t=133408

Cheers. :)

02-18-2008, 01:09 PM
I've been fiddling around with this script, I'm trying to get it to use the database instead of a text file.

if ( (!isset($_POST['scoresubmit'])) ) { ?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table width="100&#37;">
<tr><td>Your rating:</td><td><select name="rate">
<?php for ($i = 1; $i <= 10; $i++) { echo "<option value=\"$i\">$i</option>"; } ?>
<tr><td colspan="2" align="center"><input type="submit" value="Rate it!" name="scoresubmit"/></td></tr>
<?php } else {
$rate = isset ($_POST['rate']) ? $_POST['rate'] : 0;
$filename = "ratings";
$alreadyRated = false;
$totalRates = 0;
$totalPoints = 0;

$ip = getenv('REMOTE_ADDR');
$oldResults = file('results/'.$filename.'.txt');
foreach ($oldResults as $value) {
$oneRate = explode(':',$value);
if ($ip == $oneRate[0]) $alreadyRated = true;
$totalPoints += $oneRate[1];

if ((!$alreadyRated) && ($rate > 0)){
$f = fopen('results/'.$filename.".txt","a+");

<div id="result">

echo "Total score from $totalRates votes is: ".substr(($totalPoints/$totalRates),0,3)."<br/>";
for ($i=0;$i<round(($totalPoints/$totalRates),0);$i++){
echo "<img src='style/star.png' alt='s' />";
echo "</div>";

echo '
<td colspan="4">


Any ideas?

02-19-2008, 01:23 AM
Hmm well I grabbed the code, added the relevant <?php etc's to make it run, but I've never used php and html together the way they do here! Closing and reopening php while just inserting plain html in between seems very weird structure to me. Dunno where to begin mate. Actually I do I think. Instead of closing the php off and inserting plain html, do this:

echo "<html here>";

Then at least I will be able to understand and work with it ;)

For example the first lines mate, make it like this:

if ( (!isset($_POST['scoresubmit'])) ) {
echo "<form action=\"".$_SERVER['PHP_SELF']. "\" method=\"post\"><table width=\"100&#37;\"><tr><td>Your rating:</td><td><select name=\"rate\">";

So u always stay in PHP, and tell it to echo any html you need. If you wanna use $variables, just do like I've done and close off the " and use a .$whatever. then open the talking marks back up. You will notice the \"'s too. When you echo, the talking marks inside the echo must be backslashed unless you are temporarily stopping the echo to add a $variable or something.

I'm not extremely well versed in PHP but I hope this is of some use to you. Try rewriting it to not close off PHP with ?> etc and re-post if you wouldn't mind.