创建5星级系统使用PHP,MySQL和jQuery和阿贾克斯星级、系统、PHP、阿贾克斯

2023-09-10 13:30:37 作者:深巷

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

注意:未定义的变量:大鼠在C:\ XAMPP \ htdocs中\在线37等级\ rating.php

注意:未定义的变量:V IN C:\ XAMPP \ htdocs中\在线41等级\ rating.php

 < PHP
包括(settings.php文件);
连接();
$的id =阵列(1,2,3);
?>
< HTML>
< HEAD>
<脚本SRC =jquery.js和类型=文/ JavaScript的>< / SCRIPT>
    <链接相对=样式表的href =rating.css/>
<脚本类型=文/ JavaScript的SRC =rating.js>< / SCRIPT>
< /头>
<身体GT;
 < PHP
 为($ i = 0; $ I<计数($ IDS); $ I ++)
{
    $ rating_tableName ='收视率';
 的$ id = $ IDS [$ i];
 $ Q =选择total_votes,total_value从$ rating_tableName WHERE ID = $编号;
$ R =请求mysql_query($ Q);
如果(!$ R)回声mysql_error();
而($行= mysql_fetch_array($ R))
{
$ V = $行['total_votes'];
$电视= $行['total_value'];
$鼠= $电视/ $ V;

}



$ J = $ I + 1;
的$ id = $ IDS [$ i];
回声'< D​​IV CLASS =产品>
       RATE项目'。$学家
        < D​​IV ID =评级_'的$ id。'级=收视率>';
            为($ K = 1; $ K< 6; $ķ++){
                如果($鼠+ 0.5> $ K)$类=_星$ Kratings_stars ratings_vote。
                否则$类=_星$ Kratings_stars ratings_blank。
                回声'< D​​IV CLASS ='。$类。'>< / DIV>';
                }
            回声'< D​​IV CLASS =total_votes>&其中,P类=评选为>评分:'。@ number_format($大鼠)'<强&GT< / STRONG> / 5('。$ V投票投)
        < / DIV>
    < / DIV>< / DIV>';}
 ?>
< /身体GT;< / HTML>
 

解决方案

现在的问题是,因为这些变量的作用域。当你试图呼应while循环之外的变量; PHP无法找到,因为它们被创造(和分配)的varables内循环。为了解决这个问题,只是分配一个空值这两个变量之外太:

 如果(!$ R)回声mysql_error();
$大鼠= 0;
$ V = 1; //如果没有记录。
而($行= mysql_fetch_array($ R))
{
    $ V = $行['total_votes'];
    $电视= $行['total_value'];
    $鼠= $电视/ $ V;
}
 

星生活房东ios版下载 星生活房东手机客户端免费下载 v1.0

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

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

Notice: Undefined variable: v in C:\xampp\htdocs\rating\rating.php on line 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>

解决方案

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:

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

 
精彩推荐
图片推荐