...

View Full Version : 5 Star Rater



afesoccer
08-14-2007, 05:34 PM
How do I make a 5 Rating System to rate videos, pictures, etc. on my site?

wayne3503
08-14-2007, 05:46 PM
I actually have one that I modified to do 10, but if you know php well enough you should beable to modify and understand the script. Then all you need is 5 "star images". well let me know if youd like the code and ill post it for ya:D

afesoccer
08-14-2007, 06:38 PM
Yes please, I would like the code. Thank you.

wayne3503
08-14-2007, 07:10 PM
Here you go. You will see the images or "stars" I used are called spots. But all you need to do is figure what you want to use for your stars and then name the files and change the code.

<?
// User settings
$rater_ip_voting_restriction = true; // restrict ip address voting (true or false)
$rater_ip_vote_qty=1; // how many times an ip address can vote
$rater_already_rated_msg="You have already rated this item. You are allowed only ".$rater_ip_vote_qty." vote.";
$rater_not_selected_msg="You have not selected a rating value.";
$rater_thankyou_msg="Thankyou for voting.";
$rater_generic_text="this place"; // generic item text
$rater_end_of_line_char="n"; // may want to change for different operating systems


if(!isset($rater_id)) $rater_id=1;
if(!isset($rater_item_name)) $rater_item_name=$rater_generic_text;


// DO NOT MODIFY BELOW THIS LINE
$rater_filename='item_'.$rater_id.".rating";
$rater_rating=0;
$rater_stars="";
$rater_stars_txt="";
$rater_rating=0;
$rater_votes=0;
$rater_msg="";

// Rating action
if(isset($_REQUEST["rate".$rater_id])){
if(isset($_REQUEST["rating_".$rater_id])){
while(list($key,$val)=each($_REQUEST["rating_".$rater_id])){
$rater_rating=$val;
}
$rater_ip = getenv("REMOTE_ADDR");
$rater_file=fopen($rater_filename,"a+");
$rater_str="";
$rater_str = rtrim(fread($rater_file, 1024*8),$rater_end_of_line_char);
if($rater_str!=""){
if($rater_ip_voting_restriction){
$rater_data=explode($rater_end_of_line_char,$rater_str);
$rater_ip_vote_count=0;
foreach($rater_data as $d){
$rater_tmp=explode("|",$d);
$rater_oldip=str_replace($rater_end_of_line_char,"",$rater_tmp[1]);
if($rater_ip==$rater_oldip){
$rater_ip_vote_count++;
}
}
if($rater_ip_vote_count > ($rater_ip_vote_qty - 1)){
$rater_msg=$rater_already_rated_msg;
}else{
fwrite($rater_file,$rater_rating."|".$rater_ip.$rater_end_of_line_char);
$rater_msg=$rater_thankyou_msg;
}
}else{
fwrite($rater_file,$rater_rating."|".$rater_ip.$rater_end_of_line_char);
$rater_msg=$rater_thankyou_msg;
}
}else{
fwrite($rater_file,$rater_rating."|".$rater_ip.$rater_end_of_line_char);
$rater_msg=$rater_thankyou_msg;
}
fclose($rater_file);
}else{
$rater_msg=$rater_not_selected_msg;
}
}

// Get current rating
if(is_file($rater_filename)){
$rater_file=fopen($rater_filename,"r");
$rater_str="";
$rater_str = fread($rater_file, 1024*8);
if($rater_str!=""){
$rater_data=explode($rater_end_of_line_char,$rater_str);
$rater_votes=count($rater_data)-1;
$rater_sum=0;
foreach($rater_data as $d){
$d=explode("|",$d);
$rater_sum+=$d[0];
}
$rater_rating=number_format(($rater_sum/$rater_votes), 2, '.', '');
}
fclose($rater_file);
}else{
$rater_file=fopen($rater_filename,"w");
fclose($rater_file);
}

// Assign star image
if ($rater_rating <= 0){$rater_stars = "./img/0spot.gif";$rater_stars_txt="Not Rated";}
if ($rater_rating >= 1){$rater_stars = "./img/1spot.gif";$rater_stars_txt="1";}
if ($rater_rating >= 2){$rater_stars = "./img/2spot.gif";$rater_stars_txt="2";}
if ($rater_rating >= 3){$rater_stars = "./img/3spot.gif";$rater_stars_txt="3";}
if ($rater_rating >= 4){$rater_stars = "./img/4spot.gif";$rater_stars_txt="4";}
if ($rater_rating >= 5){$rater_stars = "./img/5spot.gif";$rater_stars_txt="5";}
if ($rater_rating >= 6){$rater_stars = "./img/6spot.gif";$rater_stars_txt="6";}
if ($rater_rating >= 7){$rater_stars = "./img/7spot.gif";$rater_stars_txt="7";}
if ($rater_rating >= 8){$rater_stars = "./img/8spot.gif";$rater_stars_txt="8";}
if ($rater_rating >= 9){$rater_stars = "./img/9spot.gif";$rater_stars_txt="9";}
if ($rater_rating >= 10){$rater_stars = "./img/10spot.gif";$rater_stars_txt="10";}

// Output
echo '<div class="hreview">';
echo '<form method="post" action="'.$_SERVER["PHP_SELF"].'">';
echo '<h3 class="item">Rate <span class="fn">'.$rater_item_name.'</span></h3>';
echo '<div>';
echo '<span class="rating"><img src="'.$rater_stars.'?x='.uniqid((double)microtime()*1000000,1).'" alt="'.$rater_stars_txt.' spot" /> Average rating</span>.';
echo '</div>';
echo '<div>';
echo '<label for="rate1_'.$rater_id.'"><input type="radio" value="1" name="rating_'.$rater_id.'[]" id="rate1_'.$rater_id.'" />Worst Ever</label></br>';
echo '<label for="rate2_'.$rater_id.'"><input type="radio" value="2" name="rating_'.$rater_id.'[]" id="rate2_'.$rater_id.'" />Poor</label></br>';
echo '<label for="rate3_'.$rater_id.'"><input type="radio" value="3" name="rating_'.$rater_id.'[]" id="rate3_'.$rater_id.'" />Ok</label></br>';
echo '<label for="rate4_'.$rater_id.'"><input type="radio" value="4" name="rating_'.$rater_id.'[]" id="rate4_'.$rater_id.'" />Not to Bad</label></br>';
echo '<label for="rate5_'.$rater_id.'"><input type="radio" value="5" name="rating_'.$rater_id.'[]" id="rate5_'.$rater_id.'" />Good</label></br>';
echo '<label for="rate6_'.$rater_id.'"><input type="radio" value="6" name="rating_'.$rater_id.'[]" id="rate6_'.$rater_id.'" />Very Good</label></br>';
echo '<label for="rate7_'.$rater_id.'"><input type="radio" value="7" name="rating_'.$rater_id.'[]" id="rate7_'.$rater_id.'" />Excellent</label></br>';
echo '<label for="rate8_'.$rater_id.'"><input type="radio" value="8" name="rating_'.$rater_id.'[]" id="rate8_'.$rater_id.'" />Amazing</label></br>';
echo '<label for="rate9_'.$rater_id.'"><input type="radio" value="9" name="rating_'.$rater_id.'[]" id="rate9_'.$rater_id.'" />Best Ever</label></br>';
echo '<label for="rate10_'.$rater_id.'"><input type="radio" value="10" name="rating_'.$rater_id.'[]" id="rate10_'.$rater_id.'" />HOTT</label></br>';
echo '<input type="hidden" name="rs_id" value="'.$rater_id.'" />';
echo '<input type="submit" name="rate'.$rater_id.'" value="Rate" />';
echo '</div>';
if($rater_msg!="") echo "<div>".$rater_msg."</div>";
echo '</form>';
echo '</div>';

?>

Have fun ;)

afesoccer
08-14-2007, 09:25 PM
Ohh I see. Where do I put this code?

wayne3503
08-14-2007, 09:41 PM
just save it as its own script somewhere then just do an include then have it do an echo or something. I havent played with it a whole lot as I have had bigger fish to fry with my project... so you will have to play with it a little bit to get it just as you like. But what will happen is you fill out the form and place a rating then hit the submit button and it will get echoed. But even better, it will echo out the "average" rating which is really nice.
Good luck
keep me posted with how your doing.

afesoccer
08-14-2007, 10:08 PM
Ok I saved as rater.php. Is that what I was suppose to do? How do I do the include?

Inigoesdr
08-14-2007, 10:11 PM
<?php
include('rater.php'); // change the path if it is in a different directory
?>

wayne3503
08-14-2007, 10:14 PM
The script has a form built in so you will have to incorporate everything how youd like it to be.

afesoccer
08-14-2007, 10:19 PM
<?php
include('rater.php'); // change the path if it is in a different directory
?> Do I put that in the page where the video is?

Inigoesdr
08-14-2007, 10:29 PM
Yes, I would think so. And your file will have to be named .php or at least parsed by PHP.

wayne3503
08-14-2007, 10:30 PM
put that at the top of the page you want to include it in. but you dont need the <?php ?> tags if you already have them in your video script

afesoccer
08-15-2007, 01:02 AM
http://www.afesoccer.com/rater.html That's where I put the <?php
include('rater.php'); // change the path if it is in a different directory
?>

The rater.php I uploaded to www.afesoccer.com/rater.php. But there is nothing on www.afesoccer.com/rater.html.

Inigoesdr
08-15-2007, 01:53 AM
And your file will have to be named .php or at least parsed by PHP.

:thumbsup:

afesoccer
08-16-2007, 03:09 AM
I don't understand.

rafiki
08-16-2007, 03:13 AM
change the extension from html to php and the script will get parsed by php and give you output :)

afesoccer
08-16-2007, 03:14 AM
I did. It's rater.php

afesoccer
08-16-2007, 04:08 AM
I am really not getting this. Can anyone please if they can record a video tutorial and post? Please? You can do it by downloading this program. http://www.camstudio.org/ Thank you!

afesoccer
08-17-2007, 05:12 AM
http://www.afesoccer.com/rater.php That's where the rater.php is.

StupidRalph
08-17-2007, 11:51 AM
Yes, I would think so. And your file will have to be named .php or at least parsed by PHP.


http://www.afesoccer.com/rater.html That's where I put the <?php
include('rater.php'); // change the path if it is in a different directory
?>

The rater.php I uploaded to www.afesoccer.com/rater.php. But there is nothing on www.afesoccer.com/rater.html.


I did. It's rater.php


change the extension from html to php and the script will get parsed by php and give you output :)

No you didn't change it from .html to .php. (Atleast not from what I am inferring from this post.) You have a rater.php AND a rater.html. You are trying to include rater.php inside of your rater.html page using PHP. But you can't use PHP inside of an .html page because it will not be parsed by the PHP engine (though you can set it up to do so but thats out of the scope of this thread).

You will have to rename atleast one or both of your files b/c they both can't have the name rater.php. But both need a .php extension. I have Camstudio but I'm not and( due to your lack of responses)others won't make a video as its explained thoroughly here. You may have to reread what is being said to fully grasp it. But I assure you that reading the post for understanding will help you.

Cheers and good luck
Ralph

afesoccer
08-18-2007, 12:32 AM
ohh ok. thanks

afesoccer
08-18-2007, 01:20 AM
Cool it works, http://www.afesoccer.com/rater.php thats where the <?php
include('ratercode.php'); // change the path if it is in a different directory
?> and http://www.afesoccer.com/ratercode.php is where the codes is located at. However what are those "Warning: fopen(item_1.rating) [function.fopen]: failed to open stream: Permission denied in /home/afesocce/public_html/ratercode.php on line 83

Warning: fclose(): supplied argument is not a valid stream resource in /home/afesocce/public_html/ratercode.php on line 84"?

Inigoesdr
08-18-2007, 01:26 AM
If the file(item_1.rating) exists check it's permissions to make sure PHP can access it. You should probably PM Wayne about this..

afesoccer
08-18-2007, 01:45 AM
Yeah, because I didn't get any files with it or something.

wayne3503
08-18-2007, 03:40 PM
you can take the "HOTT" rating if youd like. i just threw that in because of my site...lol...so far so good. keep it up

afesoccer
08-18-2007, 04:47 PM
Yup, but what's with the errors? Also what's your site so I could take a look of how yours looks like?

wayne3503
08-18-2007, 11:22 PM
Honestly if I still have it on my server I think I got as far as you, then I realized I had bigger projects that needed to be done for my site. My site is still a WIP right now.

NOTE: just did some digging. Here ya go :D
http://www.millermachinecompany.com/hotspot/rate.php

afesoccer
08-19-2007, 03:25 AM
Could I use what you exactly have? Also, would you know why those errors are there?

wayne3503
08-19-2007, 06:12 PM
Here ya go. This is the code off my server. No imnot really sure why your getting the errors, but this version of the code doesnt cause errors for me so maybe this will fix it for you to.

<?
// User settings
$rater_ip_voting_restriction = true; // restrict ip address voting (true or false)
$rater_ip_vote_qty=1; // how many times an ip address can vote
$rater_already_rated_msg="You have already rated this item. You are allowed only ".$rater_ip_vote_qty." vote.";
$rater_not_selected_msg="You have not selected a rating value.";
$rater_thankyou_msg="Thankyou for voting.";
$rater_generic_text="this place"; // generic item text
$rater_end_of_line_char="n"; // may want to change for different operating systems


if(!isset($rater_id)) $rater_id=1;
if(!isset($rater_item_name)) $rater_item_name=$rater_generic_text;


// DO NOT MODIFY BELOW THIS LINE
$rater_filename='item_'.$rater_id.".rating";
$rater_rating=0;
$rater_stars="";
$rater_stars_txt="";
$rater_rating=0;
$rater_votes=0;
$rater_msg="";

// Rating action
if(isset($_REQUEST["rate".$rater_id])){
if(isset($_REQUEST["rating_".$rater_id])){
while(list($key,$val)=each($_REQUEST["rating_".$rater_id])){
$rater_rating=$val;
}
$rater_ip = getenv("REMOTE_ADDR");
$rater_file=fopen($rater_filename,"a+");
$rater_str="";
$rater_str = rtrim(fread($rater_file, 1024*8),$rater_end_of_line_char);
if($rater_str!=""){
if($rater_ip_voting_restriction){
$rater_data=explode($rater_end_of_line_char,$rater_str);
$rater_ip_vote_count=0;
foreach($rater_data as $d){
$rater_tmp=explode("|",$d);
$rater_oldip=str_replace($rater_end_of_line_char,"",$rater_tmp[1]);
if($rater_ip==$rater_oldip){
$rater_ip_vote_count++;
}
}
if($rater_ip_vote_count > ($rater_ip_vote_qty - 1)){
$rater_msg=$rater_already_rated_msg;
}else{
fwrite($rater_file,$rater_rating."|".$rater_ip.$rater_end_of_line_char);
$rater_msg=$rater_thankyou_msg;
}
}else{
fwrite($rater_file,$rater_rating."|".$rater_ip.$rater_end_of_line_char);
$rater_msg=$rater_thankyou_msg;
}
}else{
fwrite($rater_file,$rater_rating."|".$rater_ip.$rater_end_of_line_char);
$rater_msg=$rater_thankyou_msg;
}
fclose($rater_file);
}else{
$rater_msg=$rater_not_selected_msg;
}
}

// Get current rating
if(is_file($rater_filename)){
$rater_file=fopen($rater_filename,"r");
$rater_str="";
$rater_str = fread($rater_file, 1024*8);
if($rater_str!=""){
$rater_data=explode($rater_end_of_line_char,$rater_str);
$rater_votes=count($rater_data)-1;
$rater_sum=0;
foreach($rater_data as $d){
$d=explode("|",$d);
$rater_sum+=$d[0];
}
$rater_rating=number_format(($rater_sum/$rater_votes), 2, '.', '');
}
fclose($rater_file);
}else{
$rater_file=fopen($rater_filename,"w");
fclose($rater_file);
}

// Assign star image
if ($rater_rating <= 0){$rater_stars = "./img/0spot.gif";$rater_stars_txt="Not Rated";}
if ($rater_rating >= 1){$rater_stars = "./img/1spot.gif";$rater_stars_txt="1";}
if ($rater_rating >= 2){$rater_stars = "./img/2spot.gif";$rater_stars_txt="2";}
if ($rater_rating >= 3){$rater_stars = "./img/3spot.gif";$rater_stars_txt="3";}
if ($rater_rating >= 4){$rater_stars = "./img/4spot.gif";$rater_stars_txt="4";}
if ($rater_rating >= 5){$rater_stars = "./img/5spot.gif";$rater_stars_txt="5";}
if ($rater_rating >= 6){$rater_stars = "./img/6spot.gif";$rater_stars_txt="6";}
if ($rater_rating >= 7){$rater_stars = "./img/7spot.gif";$rater_stars_txt="7";}
if ($rater_rating >= 8){$rater_stars = "./img/8spot.gif";$rater_stars_txt="8";}
if ($rater_rating >= 9){$rater_stars = "./img/9spot.gif";$rater_stars_txt="9";}
if ($rater_rating >= 10){$rater_stars = "./img/10spot.gif";$rater_stars_txt="10";}

// Output
echo '<div class="hreview">';
echo '<form method="post" action="'.$_SERVER["PHP_SELF"].'">';
echo '<h3 class="item">Rate <span class="fn">'.$rater_item_name.'</span></h3>';
echo '<div>';
echo '<span class="rating"><img src="'.$rater_stars.'?x='.uniqid((double)microtime()*1000000,1).'" alt="'.$rater_stars_txt.' spot" /> Average rating</span>.';
echo '</div>';
echo '<div>';
echo '<label for="rate1_'.$rater_id.'"><input type="radio" value="1" name="rating_'.$rater_id.'[]" id="rate1_'.$rater_id.'" />Worst Ever</label></br>';
echo '<label for="rate2_'.$rater_id.'"><input type="radio" value="2" name="rating_'.$rater_id.'[]" id="rate2_'.$rater_id.'" />Poor</label></br>';
echo '<label for="rate3_'.$rater_id.'"><input type="radio" value="3" name="rating_'.$rater_id.'[]" id="rate3_'.$rater_id.'" />Ok</label></br>';
echo '<label for="rate4_'.$rater_id.'"><input type="radio" value="4" name="rating_'.$rater_id.'[]" id="rate4_'.$rater_id.'" />Not to Bad</label></br>';
echo '<label for="rate5_'.$rater_id.'"><input type="radio" value="5" name="rating_'.$rater_id.'[]" id="rate5_'.$rater_id.'" />Good</label></br>';
echo '<label for="rate6_'.$rater_id.'"><input type="radio" value="6" name="rating_'.$rater_id.'[]" id="rate6_'.$rater_id.'" />Very Good</label></br>';
echo '<label for="rate7_'.$rater_id.'"><input type="radio" value="7" name="rating_'.$rater_id.'[]" id="rate7_'.$rater_id.'" />Excellent</label></br>';
echo '<label for="rate8_'.$rater_id.'"><input type="radio" value="8" name="rating_'.$rater_id.'[]" id="rate8_'.$rater_id.'" />Amazing</label></br>';
echo '<label for="rate9_'.$rater_id.'"><input type="radio" value="9" name="rating_'.$rater_id.'[]" id="rate9_'.$rater_id.'" />Best Ever</label></br>';
echo '<label for="rate10_'.$rater_id.'"><input type="radio" value="10" name="rating_'.$rater_id.'[]" id="rate10_'.$rater_id.'" />HOTT</label></br>';
echo '<input type="hidden" name="rs_id" value="'.$rater_id.'" />';
echo '<input type="submit" name="rate'.$rater_id.'" value="Rate" />';
echo '</div>';
if($rater_msg!="") echo "<div>".$rater_msg."</div>";
echo '</form>';
echo '</div>';

?>

StupidRalph
08-19-2007, 07:09 PM
If you would like to use a simple icon rating system that utilizes a DB opposed to using flat files I can email you a version that I created that uses AJAX.

afesoccer
08-19-2007, 07:14 PM
Thanks wayne3503 and yes can you send me it please StupidRalph.

wayne3503
08-19-2007, 08:01 PM
i would also be interested in seeing it to if thats ok?

afesoccer
08-21-2007, 03:50 PM
Where are you StupidRalph?

wayne3503
08-22-2007, 03:35 PM
so how is this coming along for ya so far?

afesoccer
08-22-2007, 11:04 PM
I still can't get rid of the errors.

StupidRalph
08-22-2007, 11:27 PM
I'm here...I'll PM you a link since I don't have your emails.

Sent.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum