Tengo un sistema de valoración de 5 estrellas pero en calificación media y entero equivalente del siguiente ejemplo:
0.5
1
1.5
2
2.5
3
3.5
4
4.5
5
Tengo un problema matemático y algo de lógica al mostrar los resultados por porcentajes:
//Este es el id del producto definido para realizar pruebas
$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,
AVG(rating) AS avg_rating
FROM ratings
WHERE id_product=?");
$stmt->bind_param("i",$id_product);
$stmt->execute();
$stmt->bind_result($pct_1_star);
while ($stmt->fetch()) {
echo $pct_1_star;
}
De la siguiente consulta me muestra el siguiente error:
Warning: mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement in C:\xampp\htdocs\votos\rating.php on line 19 la linea 19 equivale al siguiente prodigo
$stmt->bind_result($pct_1_star);
Ahora mi pregunta
¿Cómo puedo calcular los votos correctamente por porcentajes?
¿Cómo puedo obtener la evaluación final del producto?
* {
box-sizing: border-box;
}
table {
table-layout: fixed;
width: 100%;
border: 1px solid #ccc;
border-collapse: collapse;
}
col.small {
width: 30%;
}
col.big {
width: 10%;
}
th, td {
border: 1px solid #ccc;
border-collapse: collapse;
padding: 4px;
}
.star-icon {
color: #ddd;
font-size: 2em;
position: relative;
display: inline;
direction: rtl;
unicode-bidi: bidi-override;
}
.star-icon:before{
content: '\2605';
position: absolute;
left: 0;
}
.star-icon.full:before {
color: #fde16d;
content: '\2605';
position: absolute;
left: 0;
}
<table>
<col class="small" />
<col class="big" />
<thead>
<tr>
<th>Title</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td><ul class="star-rating">
<li class="star-icon full">☆</li>
<li class="star-icon full">☆</li>
<li class="star-icon full">☆</li>
<li class="star-icon full">☆</li>
<li class="star-icon full">☆</li>
</ul></td>
<td class="percent">40%</td>
</tr>
<tr>
<td><ul class="star-rating">
<li class="star-icon full">☆</li>
<li class="star-icon full">☆</li>
<li class="star-icon full">☆</li>
<li class="star-icon full">☆</li>
</ul></td>
<td class="percent">20%</td>
</tr>
<tr>
<td><ul class="star-rating">
<li class="star-icon full">☆</li>
<li class="star-icon full">☆</li>
<li class="star-icon full">☆</li>
</ul></td>
<td class="percent">10%</td>
</tr>
<tr>
<td><ul class="star-rating">
<li class="star-icon full">☆</li>
<li class="star-icon full">☆</li>
</ul></td>
<td class="percent">10%</td>
</tr>
<tr>
<td><ul class="star-rating">
<li class="star-icon full">☆</li>
</ul></td>
<td class="percent">20%</td>
</tr>
</tbody>
</table>