I have a 5-star rating system but in average and integer rating equivalent to the following example:
0.5
1
1.5
2
2.5
3
3.5
4
4.5
5
I have a mathematical problem and some logic when displaying the results by percentages:
//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;
}
The following query shows me the following 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 line 19 is equivalent to the following prodigy
$stmt->bind_result($pct_1_star);
now my question
How can I calculate the votes correctly by percentages?
How can I get the final evaluation of the product?
* {
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>
As the message says, you have a small error, you are not correctly binding in your
bind_result
data that you want to obtain from your prepared statement. If you look closely you are calculating 6 data, since you have 5sum()
and oneAVG
at the end.Modified example:
How can I calculate the votes correctly by percentages?
Remember that your statement is calculating these values.
How can I get the final evaluation of the product?
AVG
, that is, it returns the average of the data values, that is, you are calculating it with the last line of your statementAVG(rating) AS avg_rating
.How can I get the results like the following picture?
Here I also leave you a small example of w3schools
Complete PHP MySQL example: