Ajmal Razeel Ajmal Razeel - 2 months ago 7x
MySQL Question

how to display numbers from MySQL database as stars with PHP Codeigniter

I have a table that stores feedbacks about tutors and the rating (out of 5) which other users give them.

Following is my code where I fetch data from database:

<?php foreach ($tut_feedback as $tf) { ?>
<div class="">
<h4 class="title"><strong><?php echo $tf->std_name;</strong></h4>
<span class="meta"><?php echo $tf->newdate_time; ?></span>
<span class="rating"><?php echo $tf->tutor_rating; ?></span> // display numbers like 2,3,4
<p class="description"><?php echo $tf->feedback_description; ?></p>
<?php } ?>

My question is how can I display the numbers as stars. For example, if a tutor has rating of 4, I want to display 4 stars instead of the number..


Based on the tutor rating you can run one for loop:-

$rating = $tf->tutor_rating;
for($i=0; $i<$rating; $i++) {
    echo "<img src='star.png' alt='*'>";