繁体   English   中英

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

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

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

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

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

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

如果您在全局(循环外)声明它们,则其余代码将识别它们。

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

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

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

在给出评级之后显示叠加DIV,使得不能给出立即评级。 它还将用户的IP地址与评级提交一起存储,以防止来自一个用户的多个评级。

这是一个简单易用的脚本,只有Javascript和PHP的星级评级。

问题是因为这些变量的范围。 当你试图在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;
}

在开头的行中添加此内容,在代码中注意错误。

error_reporting(E_ALL ^ E_NOTICE);

大多数时间通知错误不会影响程序。 如果您的投票没有录制,则删除您的cookie并尝试从不同的IP地址投票。此脚本具有不接受来自相同IP或vistitor的投票的功能,以避免同一用户在同一产品上多次投票。

  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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM