5

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

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

注意:未定义的变量:C:\xampp\htdocs\rating\rating.php 中的 v 第 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>
4

6 回答 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;
}
于 2013-06-08T22:20:32.043 回答
1

见这里: http ://bgallz.org/988/javascript-php-star-rating-script/

这结合了一个 Javascript 代码,该代码为给定的不同评级生成 URL,以及在给定评级之前和之后星星的显示变化。

给出评级后会显示一个覆盖 DIV,因此不能立即给出评级。它还将用户的 IP 地址与评级提交一起存储,以防止一个用户的多个评级。

这是一个简单易用的脚本,仅使用 Javascript 和 PHP 进行星级评分。

于 2013-09-30T15:41:39.000 回答
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;
}
于 2013-06-08T22:19:52.730 回答
0

在开头的行添加此代码以消除代码中的通知错误。

error_reporting(E_ALL ^ E_NOTICE);

大多数时候通知错误不会影响程序。如果您的投票没有记录,请删除您的 cookie 并尝试从不同的 IP 地址投票。此脚本具有不接受来自同一 IP 或访问者的投票的功能,以避免同一用户对同一产品进行多次投票。

于 2013-06-09T09:00:37.107 回答
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的代码

于 2016-02-26T18:46:09.907 回答
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>
于 2016-11-29T05:13:03.813 回答