2017-10-15 10 views
0

私はメディアの製品と同等の星の評価のシステムを持っており、全体どのように製品の評価システムの正しい割合を計算する?

例:

0.5から1 - 1.5 - 2から2.5 - 3から3.5 - 4から4.5 - 5

2つのデータを中と全体で正しく取り込み、両方の値を合計します。

次の相談から

//we define in a variable the id of the product valued by the valuation system 
$id_product = 1; 

//we obtain an average and integer value equivalent to 4.5 and 5 
$stmt = $con->prepare("SELECT SUM(rating) as total FROM ratings WHERE id_product=? AND rating in(4.5,5)"); 
$stmt->bind_param("i",$id_product); 

$stmt->execute(); 
$stmt->bind_result($sum); 

while ($stmt->fetch()) { 
    echo $sum; 
} 

//we obtain an average and integer value equivalent to 3.5 and 4 
$stmt = $con->prepare("SELECT SUM(rating) as total FROM ratings WHERE id_product=? AND rating in(3.5,4)"); 
$stmt->bind_param("i",$id_product); 

$stmt->execute(); 
$stmt->bind_result($sum); 

while ($stmt->fetch()) { 
    echo $sum; 
} 

//we obtain an average and integer value equivalent to 2.5 and 3 
$stmt = $con->prepare("SELECT SUM(rating) as total FROM ratings WHERE id_product=? AND rating in(2.5,3)"); 
$stmt->bind_param("i",$id_product); 

$stmt->execute(); 
$stmt->bind_result($sum); 

while ($stmt->fetch()) { 
    echo $sum; 
} 

//we obtain an average and integer value equivalent to 1.5 and 2 
$stmt = $con->prepare("SELECT SUM(rating) as total FROM ratings WHERE id_product=? AND rating in(1.5,2)"); 
$stmt->bind_param("i",$id_product); 

$stmt->execute(); 
$stmt->bind_result($sum); 

while ($stmt->fetch()) { 
    echo $sum; 
} 

//we obtain an average and integer value equivalent to 0.5 and 1 
$stmt = $con->prepare("SELECT SUM(rating) as total FROM ratings WHERE id_product=? AND rating in(0.5,1)"); 
$stmt->bind_param("i",$id_product); 

$stmt->execute(); 
$stmt->bind_result($sum); 

while ($stmt->fetch()) { 
    echo $sum; 
} 

今、私の質問はどのように私が正しく割合でデータを計算し、以下の画像のような結果を得ることができます:

enter image description here

+0

「相撲」? 「苦闘する」のように? – Strawberry

+0

@Strawberryはそれほど深刻ではありません正書法上のエラーは「合計」 –

答えて

1

その中のパーセンテージテーブルは、評価値の合計ではなく、カウントに基づいています。

これをすべて1つのクエリで実行できます。評価範囲ごとに個別のクエリを実行する必要はありません。

$stmt = $con->prepare(' 
    SELECT SUM(rating IN (4.5, 5))/COUNT(*)*100 AS pct_5_star, 
      SUM(rating IN (3.5, 4))/COUNT(*)*100 AS pct_4_star, 
      SUM(rating IN (2.5, 3))/COUNT(*)*100 AS pct_3_star, 
      SUM(rating IN (1.5, 2))/COUNT(*)*100 AS pct_2_star, 
      SUM(rating IN (0.5, 1))/COUNT(*)*100 AS pct_1_star, 
      AVG(rating) AS avg_rating 
    FROM ratings 
    WHERE id_product = ?'); 
$stmt->bind_param('i', $id_product); 
$stmt->execute(); 
$percents = array(; 
$stmt->bind_result($percents[5], $percents[4], $percents[3], $percents[2], $percents[1], $avg_rating); 
if ($stmt->fetch()) { 
    for ($i in array(5, 4, 3, 2, 1)) { 
     echo "%i stars: " . round($percents[$i]) . "%<br>"; 
    } 
    echo "Average rating: $avg_rating<br>"; 
} else { 
    echo "This product is not yet rated<br>"; 
} 
+0

を意味し、そのようにして個々のパーセンテージを画像として表示する方法、つまりいくつかの顧客が星の10%を投票するなど – Alex

+0

'pct _#_ star'に依存するバーの長さ。 – Barmar

+0

PHPには、特定のサイズの矩形を作成するイメージ関数があります。 – Barmar

関連する問題