2012-01-07 91 views
0

我想让我的评分系统进行下去,这将成为我网站的基础。我如何使用+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">&nbsp; 
<input type="radio" name="new_ratings_value" value="2" />2<img src="small_star.png">&nbsp; 
<input type="radio" name="new_ratings_value" value="3" />3<img src="small_star.png">&nbsp; 
<input type="radio" name="new_ratings_value" value="4" />4<img src="small_star.png">&nbsp; 
<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"> 
+0

用户上面的代码中做到这一点... 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

回答

0

你需要创建一个单独的表这一点。列将是

user_id (int) 
product_id (int) 
rating (int) 

或者,您也可以有一个ID字段,但它不是强制性的。然后,要获得评分,请根据产品ID使用mysql的AVG()函数选择平均分数。记住要更新一个人的等级,如果他们以前

当然,这假定您的应用程序与用户ID的

+0

一个单独的表?我正在考虑拥有数千种不同的物品,那将如何工作?我之前和一个人一起工作过,他们基本上告诉我要更新 - 也就是说,要改变......当前值加上这个数字值,但那个人不可用,并且开始变得混乱。 – 2012-01-07 21:17:28

+0

所有我需要做的...如果我不清楚... ...是每个提交,这是输入类型=“隐藏”进来,并添加1到5(基于根据用户的评价)到已经存在的ratings_value列。 – 2012-01-07 21:19:59

+1

你*可以*做到这一点,但这样做的问题是,你没有保留** who **提供评分的记录。这使得你的系统变得毫无价值,因为如何阻止某个真正喜欢或者真正讨厌某个产品的人重复评估它? – Hammerite 2012-01-07 21:21:00