2012-01-07 45 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