5

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>
Sebas
  • 21,192
  • 9
  • 55
  • 109
John
  • 107
  • 1
  • 3
  • 15
  • Please don't forget to mention the framework you're working with whenever you do (I edited the tags of this question to reflect yours). This is important since the tag filtering system of stackoverflow relies on this. Thank you. – Sebas Jun 08 '13 at 22:12
  • Could you echo `$q` and confirm whether there are returned rows? `$rat` would not be initialized if the recordset is empty. (the same applies to `$v`) – Sebas Jun 08 '13 at 22:15
  • I did it, and it shows me this: SELECT total_votes, total_value FROM ratings WHERE id=1 – John Jun 08 '13 at 22:18
  • Ok, I suppose you also manually checked this query in your mysql client? I think these variables should be initialized anyway, even if there's no record found. See my point? – Sebas Jun 08 '13 at 22:19
  • yes, I'm following you, thanks for the help, it's working now – John Jun 08 '13 at 22:27

6 Answers6

1

$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;
}
faide
  • 11
  • 1
1

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.

bgallz
  • 111
  • 6
0

The problem is because of scoping of those variables. When you are trying to echo those variables outside the while loop; PHP can not find the varables as they were created (and assigned) inside the loop. To solve this, just assign a blank value to both variables outside too:

if(!$r) echo mysql_error();
$rat = 0;
$v = 1;    // In case there are no records.
while($row=mysql_fetch_array($r))
{
    $v = $row['total_votes'];
    $tv = $row['total_value'];
    $rat = $tv/$v;
}
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
0

Add this at line at beginning to nide notice error in your code .

error_reporting(E_ALL ^ E_NOTICE);

Most of time notice error do not affect the program. In case if your votes are not recording then delete your cookies and try to vote from different IP address .This script has a feature to not accept votes from same ip or vistitor to avoid multiple votes by same users on same product.

amit
  • 250
  • 1
  • 2
  • 10
0
  var cname=document.getElementById(id).className;
  var ab=document.getElementById(id+"_hidden").value;
  document.getElementById(cname+"rating").innerHTML=ab;

  for(var i=ab;i>=1;i--)
  {
     document.getElementById(cname+i).src="star2.png";
  }
  var id=parseInt(ab)+1;
  for(var j=id;j<=5;j++)
  {
     document.getElementById(cname+j).src="star1.png";
  }

Code from http://talkerscode.com/webtricks/star-rating-system-using-php-and-javascript.php

rahul
  • 1
  • 1
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – mech Feb 26 '16 at 19:07
0
<style>
.star {
    font-size: x-large;
    width: 50px;
    display: inline-block;
    color: gray;
}
.star:last-child {
    margin-right: 0;
}
.star:before {
    content:'\2605';
}
.star.on {
    color: red;
}
.star.half:after {
    content:'\2605';
    color: red;
    position: absolute;
    margin-left: -20px;
    width: 10px;
    overflow: hidden;
}
</style>
<div class="stars"> 
<?php 
    $enable = 5.5;  //enter how many stars to enable
    $max_stars = 6; //enter maximum no.of stars
    $star_rate = is_int($enable) ? 1 : 0;
    for ($i = 1; $i <= $max_stars; $i++){ ?>
    <?php if(round($enable) == $i && !$star_rate) { ?>
            <span class="<?php echo 'star half'; ?>"></span>
    <?php } elseif(round($enable) >= $i) { ?>
            <span class="<?php echo 'star on'; ?>"></span>
    <?php } else { ?>
        <span class="<?php echo 'star'; ?>"></span>
    <?php } 
    }?>
</div>