I have an error in wanting to add an error message when there are no votes for any product.
When there are no votes in a product, all the variables in the query return the print value of 0
, apparently taking the value 0
as a valid data as if it were a vote of value 0
How can I create a condition for the variables or the while ($stmt->fetch())
that if all the values in the query return the value 0
this shows the error message?
Note: That the condition or parameter is only executed if the entire query of its variables returns value
0
if there is any numeric value of0.5
not executed.
$id_product = 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,
SUM(CASE WHEN rating IN (4.5, 5) THEN rating ELSE 0 END) AS in_5_star,
AVG(rating) AS avg_rating
FROM ratings
WHERE id_product=?");
$stmt->bind_param("i",$id_product);
$stmt->execute();
$stmt->bind_result($pct_5_star, $pct_4_star, $pct_3_star, $pct_2_star, $pct_1_star, $in_5_star, $avg_rating);
while ($stmt->fetch()) {
echo "5 estrellas " . number_format($pct_5_star). "% <br />";
echo "4 estrellas " . number_format($pct_4_star). "% <br />";
echo "3 estrellas " . number_format($pct_3_star). "% <br />";
echo "2 estrellas " . number_format($pct_2_star). "% <br />";
echo "1 estrellas " . number_format($pct_1_star). "% <br /><br />";
echo "<b>Promedio:</b> " . number_format($avg_rating,1). "% <br />";
} else {
echo "No existe votos de este producto";
}