Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating 5 Star Rating System With PHP , MySQL ,Jquery And Ajax

I've downloaded this tutorial http://megarush.net/5-star-rating-system-with-php-mysql-jquery-and-ajax/ but I'm getting these errors:

Notice: Undefined variable: rat in C:\xampp\htdocs\rating\rating.php on line 37

Notice: Undefined variable: v in C:\xampp\htdocs\rating\rating.php on line 41

<?php
include("settings.php");
connect();
$ids=array(1,2,3);
?>
<html>
<head>
<script src="jquery.js" type="text/javascript"></script>
    <link rel="stylesheet" href="rating.css" />
<script type="text/javascript" src="rating.js"></script>
</head>
<body>
 <?php
 for($i=0;$i<count($ids);$i++)
{
    $rating_tableName     = 'ratings';
 $id=$ids[$i];
 $q="SELECT total_votes, total_value FROM $rating_tableName WHERE id=$id";
$r=mysql_query($q);
if(!$r) echo mysql_error();
while($row=mysql_fetch_array($r))
{
$v=$row['total_votes'];
$tv=$row['total_value'];
$rat=$tv/$v;

}



$j=$i+1;
$id=$ids[$i];
echo'<div class="product">
       Rate Item '.$j.'
        <div id="rating_'.$id.'" class="ratings">';
            for($k=1;$k<6;$k++){
                if($rat+0.5>$k)$class="star_".$k."  ratings_stars ratings_vote";
                else $class="star_".$k." ratings_stars   ratings_blank";
                echo '<div class="'.$class.'"></div>';
                }
            echo' <div class="total_votes"><p class="voted"> Rating:     <strong>'.@number_format($rat).'</strong>/5 ('.$v. '  vote(s) cast) 
        </div>
    </div></div>';}
 ?>
</body></html>
like image 390
John Avatar asked Nov 03 '22 20:11

John


2 Answers

$rat and $v are being defined within the scope of your while loop.

If you declare them globally (outside the loop), the rest of your code will recognize them.

$rat = 0;
$v = 1;
while($row=mysql_fetch_array($r))
{
    $v=$row['total_votes'];
    $tv=$row['total_value'];
    $rat=$tv/$v;
}
like image 152
faide Avatar answered Nov 09 '22 07:11

faide


See here: http://bgallz.org/988/javascript-php-star-rating-script/

This combines a Javascript code that generated the URL for the different ratings given as well as the change in display for the stars before and after a rating is given.

An overlay DIV is displayed after the rating is given so that no immediate ratings can be given by the same. It also stores the user's IP address with the rating submission to prevent multiple ratings from one user.

This is a simple and easy to use script with just Javascript and PHP for star rating.

like image 23
bgallz Avatar answered Nov 09 '22 05:11

bgallz