我想让我的评分系统进行下去,这将成为我网站的基础。我如何使用+1(到number_of_ratings)列更新数据库,并将1到5(取决于用户输入[它们的评分])添加到ratings_value列。然后这些在PHP中分开以提出average_rating,但我相信这是正确完成的。我是新来的准备好的陈述,这就是我正在努力的。谢谢用数学公式更新数据库
<?php
include'config.php';
// Check Connection
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT brand, name, ratings_value, number_of_ratings, average_price, review, image, ratings_value FROM Products WHERE product_id = 0";
$result = $mysqli->query($query);
$row = $result->fetch_array(MYSQLI_ASSOC);
printf ("%s<br />%s<br />%s<br />%s<br />%s<br />%s\n",
$row["brand"],
$row["name"],
$average_rating = $row["ratings_value"]/$row["number_of_ratings"],
$number_of_ratings = $row["number_of_ratings"],
$row["average_price"],
$row["review"],
$ratings_value = $row["ratings_value"]);
// using this will round value to nearest quarter - 0.25 - using 3 (nearest third), using 2 (nearest half), using 10 (nearest 10th)
//$average_rating = round(($average_rating*4), 0)/4;
$average_rating = round(($average_rating),2);
$background = round($average_rating/5*120);
if ($average_rating <= 5){
print ("<div style=\"width:{$background}px; background-color:#ffff00\"><img style=\"width:120px; height:30px\" src=\"stars.png\" /></div>");
}
else {
print ("A value over 5? Not possible!<br />We are working to solve this as soon as we can");
}
?>
<form method="POST" action="womensjeggings.php">
<input type="radio" name="new_ratings_value" value="1" />1<img src="small_star.png">
<input type="radio" name="new_ratings_value" value="2" />2<img src="small_star.png">
<input type="radio" name="new_ratings_value" value="3" />3<img src="small_star.png">
<input type="radio" name="new_ratings_value" value="4" />4<img src="small_star.png">
<input type="radio" name="new_ratings_value" value="5" />5<img src="small_star.png">
<input type="hidden" name="new_number_of_ratings" value="1" />
<input type="submit" />
</form>
// womensjeggings.php - 最有可能的,远非正确
<?php
mysql_query("UPDATE Products SET ratings_value = '$ratings_value+$new_ratings_value', number_of_ratings = '$number_of_ratings+$new_number_of_ratings' WHERE product_id = 0");
?>
<meta http-equiv="refresh" content="2;url=index.php">
用户上面的代码中做到这一点... http://thegreatest.zymichost.com表决产品/index.php ...但是,评级选项不起作用,在我看来,其他任何东西都看起来不错。如果您好奇,我手动输入数据库中的数字进行测试。第一个数字(第3行)是ratings_value(数据库中的17)除以number_of_ratings(数据库中的5)以获得平均评级(第3行中的数字)。第二个数字(在第4行)是5,这是评级的数量......显示的两个数字都是我想让用户看到的数字。星星根据百分比着色3.4/5感谢您的帮助! – 2012-01-07 21:10:26