PhpSnip.com

User Stats

5 star rating system

Allows guests to rate something 1 to 5 stars, then takes the results and returns a number of stars based on the average rating. If you try the script and get an error regarding the file 'ratings.php', try downloading the zipped file at http://habitat675.port5.com/rate/ratings.zip and chmod ratings.php 666. File also includes star images which you are free to use if you like, or you can use your own.

Info

 Download  View Source (print view)
 Rating : 4.7  Views : 1420

Source Code ( 152 lines )

<HTML>
<BODY>

<?php

/* IF YOU WOULD LIKE TO CHANGE THE IMAGES then edit the paths here! */

$half_img = "half.gif";

$whole_img = "star.gif";

/* No editing is needed beyond here */

if(!file_exists('ratings.php')) {

echo "FILE DOES NOT EXIST";

$fp = fopen("ratings.php","w");

if(!$fp) {
 echo "error! the file could not be opened";
 exit;
         }
         
$contents = "<"."?php $"."one = "0"; $"."two = "0"; $"."three = "0"; $"."four = "0"; $"."five = "0"; ?".">";

fwrite($fp, $contents);
fclose($fp);

chmod ("ratings.php", 0666);

}

require("ratings.php");

if(isset($_POST['your_rating'])) {

$your_rating = $_POST['your_rating'];

}

if(isset($your_rating)) {

${$your_rating}++;

$fp = fopen("ratings.php","w");

if(!$fp) {
 echo "error! the file could not be opened";
 exit;
         }
         
$contents = "<"."?php $"."one = "$one"; $"."two = "$two"; $"."three = "$three"; $"."four = "$four"; $"."five = "$five"; ?".">";

fwrite($fp, $contents);
fclose($fp);


}

$total = $one + $two + $three + $four + $five;

if($total != "0") {

$ones = $one / $total;

$ones = 1 * $ones;

$twos = $two / $total;

$twos = 2 * $twos;

$threes = $three / $total;

$threes = 3 * $threes;

$fours = $four / $total;

$fours = 4 * $fours;

$fives = $five / $total;

$fives = 5 * $fives;

$all = $ones + $twos + $threes + $fours + $fives;

$whole = substr($all, 0, 1);

$decimal = substr($all, 1, 4);

$decimal = $decimal * 10;

if($decimal >= 3.5 && $decimal <= 7.5) {

$half = "true";

} elseif($decimal > 7.5) {

$all++;

$half = "false";

} else {

$half = "false";

}

$decimal = $decimal / 10;

$all = floor($all);

$i = 0;

while($i < $all) {

echo "<IMG src="$whole_img">";

$i++;

}

if($half == "true") {

echo "<IMG src="$half_img">";

}

} else {

echo "This has no votes yet.";

}

?>

<!-- The following can be pasted anywhere on the page. Make sure the action goes to THIS page itself -->

<FORM method="post" action="rate.php">
<SELECT name="your_rating">
<OPTION value="one">1</OPTION>
<OPTION value="two">2</OPTION>
<OPTION value="three">3</OPTION>
<OPTION value="four">4</OPTION>
<OPTION value="five">5</OPTION>
</SELECT>
 
<INPUT type="submit" value="rate!">
</FORM>

</BODY>
</HTML>

Search

Subscribe

  Rss Feeds

Sponsors

Advertise