【问题标题】:Creating 5 Star Rating System With PHP , MySQL ,Jquery And Ajax使用 PHP、MySQL、Jquery 和 Ajax 创建 5 星评级系统
【发布时间】:2013-06-04 22:57:03
【问题描述】:

我已经下载了本教程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 的标签过滤系统依赖于此。谢谢。
  • 能否回显$q并确认是否有返回行?如果记录集为空,$rat 将不会被初始化。 (同样适用于$v
  • 我做到了,它告诉我:SELECT total_votes, total_value FROM rating WHERE id=1
  • 好的,我想你也在你的mysql客户端中手动检查了这个查询?我认为无论如何都应该初始化这些变量,即使没有找到记录。明白我的意思了吗?
  • 是的,我正在关注你,感谢您的帮助,它正在工作中

标签: php jquery mysql ajax rating-system


【解决方案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;
}

【讨论】:

  • 酷,它成功了,但它没有记录投票,我该怎么办?
【解决方案2】:

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

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

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

这是一个简单易用的脚本,只需 Javascript 和 PHP 即可获得星级评分。

【讨论】:

    【解决方案3】:

    问题在于这些变量的范围。当您尝试在 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 了解更多关于恒星系统的信息。
    【解决方案4】:

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

    error_reporting(E_ALL ^ E_NOTICE);
    

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

    【讨论】:

      【解决方案5】:
        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的代码

      【讨论】:

      • 虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。
      【解决方案6】:
      <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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-06
        • 1970-01-01
        • 1970-01-01
        • 2016-06-21
        相关资源
        最近更新 更多