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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17004356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:03:51  来源:igfitidea点击:

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

phpjquerymysqlajaxrating-system

提问by John

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

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

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

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

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

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

采纳答案by hjpotter92

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:

问题在于这些变量的范围。当您尝试在 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;
}

回答by faide

$ratand $vare being defined within the scope of your whileloop.

$rat并且$v正在您的while循环范围内定义。

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

回答by bgallz

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

见这里: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.

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

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.

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

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

这是一个简单且易于使用的脚本,只需 Javascript 和 PHP 即可进行星级评分。

回答by rahul

  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

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

回答by user3909765

<style>
.star {
    font-size: x-large;
    width: 50px;
    display: inline-block;
    color: gray;
}
.star:last-child {
    margin-right: 0;
}
.star:before {
    content:'05';
}
.star.on {
    color: red;
}
.star.half:after {
    content:'05';
    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>

回答by amit

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.

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