Zend - The PHP Company




Miscellaneous

Add Code


5 star rating system  

Type: application
Added by: habitat675
Entered: 23/07/2003
Last modified: 09/12/2002
Rating: *** (6 votes)
Views: 8928
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.


<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 $ones;

$twos $two $total;

$twos $twos;

$threes $three $total;

$threes $threes;

$fours $four $total;

$fours $fours;

$fives $five $total;

$fives $fives;

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

$whole substr($all01);

$decimal substr($all14);

$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>
&nbsp;
<INPUT type="submit" value="rate!">
</FORM>

</BODY>
</HTML>


Usage Example


See the example


Rate This Script





Search



This Category All Categories