• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net

PHP - Rating Script

Damoose

New Member
Anyone willing to help me for free? Because im only 13 i am not able to pay for someone to write a script.

My site is http://www.companyreviews.net and I want visitors to be able to rate the sites by selecting from 1 to 5, anyone help me? Thankyou so much.
 
Well, if you have a database it is quite easy. If you already know some PHP that is.

Just make sure you have a column in your database, lets say you named it "rate."

Make a form similar to this:

Code:
<form action="action_page.php" method="post">
<input type="radio" name="rating" value="1">1<br>
<input type="radio" name="rating" value="2">2<br>
<input type="radio" name="rating" value="3">3<br>
<input type="radio" name="rating" value="4">4<br>
<input type="radio" name="rating" value="5">5<br>
<input type="submit" value="Vote">
</form>

Then in your action page, do a scrip similar to this (you will have to aquire the old value of "rate" in your database...)

PHP:
<?php
mysql_connect("yourserver","yourdatabase","yourpassword") or die("An error occurred.");
mysql_select_db("yourdatabase");

$get_old = mysql_query("SELECT rate FROM yourtable");
$old_data = mysql_fetch_array($get_old);

$old = $old_data[rate];   // Gets the value (number) of "rate"
$new = ($old + $rating) / 2   // "$rating" is from the form, the math is to figure out the average

mysql_query("UPDATE yourtable SET rate=$new") or die("An error occurred"); //Updating the new average.
?>

Of coarse, if you want to rate different things, you just alter the first mysql query to
Code:
$get_old = mysql_query("SELECT rate FROM yourtable WHERE sitename='sitename'");
, thats if you have a column with the site name you want to rate...

You might want to look over the syntax, I probably made a mistake.
 
Back
Top