Hi, I want to display the avearage row as images. Is it possible?

function reviews_average(){

            $avgproduct_id = escape_string($_GET['id']);

            $avgquery = "SELECT avg(rating) as average_rating FROM reviews WHERE product_id = $avgproduct_id";
            $avgresults = query($avgquery);
            confirm_query($avgquery);

            while($row = fetch_array($avgresults)){

            $avgratings = **--THIS IS THE ONE I WANT TO DISPLAY AS IMAGES--** "{$row['average_rating']}";    

            echo $avgratings; 
   }
 }

This is the image

<img src='../public/images/star.png' height='20px' width='20px'/>

Recommended Answers

All 2 Replies

I’m on my phone in bed so it’s hard to type our code but you can use php’s string repeater function.

https://www.php.net/manual/en/function.str-repeat.php

You can echo out the image html tag the number of times based on the rating.

You could do this with a simple for loop as well but this function is easier.

Were you able to figure it out? The code would be like this:

echo str_repeat("<img src='../public/images/star.png' height='20px' width='20px'/>", floor($row['average_rating']));

More information at https://www.php.net/manual/en/function.str-repeat.php

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.