使用PHP、MySQL、Jquery和Ajax创建五星级评分系统

5
我已经下载了这个教程http://megarush.net/5-star-rating-system-with-php-mysql-jquery-and-ajax/,但我遇到了以下错误:

注意: C:\xampp\htdocs\rating\rating.php的第37行未定义变量:rat

注意: C:\xampp\htdocs\rating\rating.php的第41行未定义变量:v

<?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>

请在提问时不要忘记提及您使用的框架(我已经编辑了该问题的标签以反映您的选择)。这很重要,因为stackoverflow的标签过滤系统依赖于此。谢谢。 - Sebas
你能回显 $q 并确认是否返回了行吗?如果记录集为空,则 $rat 不会被初始化。(对 $v 也适用) - Sebas
我已经完成了,并且它显示了这个:SELECT total_votes,total_value FROM ratings WHERE id = 1 - John
好的,我想你也手动在你的mysql客户端中检查了这个查询吧?我认为这些变量应该被初始化,即使没有找到记录。明白我的意思吗? - Sebas
是的,我在跟着你,感谢你的帮助,现在它可以工作了。 - John
6个回答

1

$rat$v 在你的 while 循环作用域内被定义。

如果你在循环外部全局声明它们,你的代码的其余部分将能够识别它们。

$rat = 0;
$v = 1;
while($row=mysql_fetch_array($r))
{
    $v=$row['total_votes'];
    $tv=$row['total_value'];
    $rat=$tv/$v;
}

很酷,它起作用了,但它没有记录投票,我该怎么办? - John

1
请看这里:

点击链接: http://bgallz.org/988/javascript-php-star-rating-script/

这个代码结合了Javascript,可以生成不同评级的URL,并在给出评级前后改变星级显示。

在评级后,会出现一个叠加的DIV,以防止用户立即重新评级。它还会将用户的IP地址与评级提交一起存储,以防止同一用户进行多次评级。

这是一个简单易用的脚本,只需要使用Javascript和PHP来进行星级评定。


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";
  }

来自http://talkerscode.com/webtricks/star-rating-system-using-php-and-javascript.php的代码


虽然这段代码可能回答了问题,但提供有关它如何以及/或为什么解决问题的附加上下文将改善答案的长期价值。 - mech

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>

0
问题出在变量的作用域上。当你试图在 while 循环外输出这些变量时,PHP 找不到它们,因为它们是在循环内创建(并赋值)的。要解决这个问题,只需在循环外也将这两个变量赋一个空值即可:
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;
}

很酷,它起作用了,但它没有记录投票,我该怎么办? - John
@John 请再多了解一下星级系统。 - hjpotter92

0
将以下代码添加到开头的行,以便在您的代码中发现错误:

error_reporting(E_ALL ^ E_NOTICE);

大多数情况下,注意到错误并不会影响程序。 如果您的投票没有记录,则删除您的Cookie并尝试从不同的IP地址进行投票。此脚本具有不接受来自相同IP或访问者的投票的功能,以避免同一用户在同一产品上进行多次投票。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接