A
A
Artem2020-05-11 14:52:11
PHP
Artem, 2020-05-11 14:52:11

How to display the received data with different 'id' in the same form?

Guys, tell me how to add a rating with the same 'id' for each product separately to different forms added via 'echo' by 'id'. The data 'stars_id' = '1' 'val' = '3.5' is passed through the $average1 variable for all products equally.

$result = "SELECT COUNT(stars_id), round(AVG(val),1) as average FROM stars GROUP BY stars_id"; 
$data = mysqli_query($connectBD, $result);
if (mysqli_num_rows($data)){
$row = mysqli_fetch_assoc($data);
$average1 = $row['average'];
echo $average1;

5eb93a0f42e8c098828033.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FanatPHP, 2020-05-11
@shylov

If I correctly understood the meaning of the stars_id field, then throw out COUNT and collect the result in an array:

$result = "SELECT stars_id, round(AVG(val),1) as average FROM stars GROUP BY stars_id"; 
$data = mysqli_query($connectBD, $result);
while ($row = mysql_fetch_assoc($data)) {
    $stars[$row['stars_id']] = $row['average'];
}

and then you can display the rating by id, echo $stars[$id];
But in general, in a good way, you need to get a rating immediately with the goods, in one request
select g.*, round(AVG(val),1) as stars FROM goods g, stars s WHERE stars_id=g.id GROUP BY g.id

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question