-2

So, my question is very strait forward, I have a single variable, per say 4, and I need to translate it to some kind of rating, very simple ( it goes from 1 to 5 ) without half stars or anything.

I need to display a rating out of a number which goes from one to five, so if I have 1 I will have a star, if I have 5 I will have five stars. I can do a for loop and display the images, but I also need to show the empty ( not rated ) star. I don't need any functionality to it, so you cannot actually rate, just show the ratings.

So because anyone is asking what I've tried, I haven't tried anything because I don't know where to begin.

I could do this to show a number of stars based on the variable I have ( which is a number from 1 -> 5 ):

<?php

$ratings = 3;

for( $i = 1; $i <= $ratings; $i++)
echo '<img src="some_image.png" />';

?>

This would be it, but what should I do for the non-rated starts, how do I display the rest of the empty stars till five ?

Roland
  • 9,321
  • 17
  • 79
  • 135
  • You mean you need to round the variable's value to the nearest integer? – ppp Apr 12 '12 at 20:47
  • 2
    Yea, can your ask a more specific question. What have your tried? Can you show us some of your code? – The Man Apr 12 '12 at 20:48
  • I don't need to round. And I haven't tried anything because all I could find was doing a rating system where you can rate with MySQL and PHP. But I only need to display the rating, and not rate, and I don't need MySQL, because I only have the variable which is a number between 1 and 5 which tells me with how many stars the item was rated. – Roland Apr 12 '12 at 20:50
  • @Roland You will have to make your question a little clearer, your goal is not really understandable (to me at least) – ppp Apr 12 '12 at 20:57
  • What does this mean? Please post what you have tried and what errors you are getting etc for us to look at... – danielrsmith Apr 12 '12 at 20:57
  • I'm sorry but there is nothing more to add, that's the question, if you don't understand it don't answer it. And what errors ? I'm asking how to do it not what am I doing wrong. – Roland Apr 12 '12 at 21:03
  • 1
    @Roland, you might have misunderstood the purpose of this site. Please read the [FAQ](http://stackoverflow.com/faq#questions) to find out more about what kind of questions you can ask here. – rid Apr 12 '12 at 21:04
  • I'm sorry @Radu, but what's that suppose to mean ? – Roland Apr 12 '12 at 21:05
  • 1
    @Roland, I see you asked quite a few questions before, so you should have a good grasp of what constitutes an accepted question. Basically, the idea is that you should ask a specific programming question. This question is different in the fact that it asks the community to code for you. If you try to code that and run into trouble, or if you need advice at certain stages of your implementation, by all means, please do post a specific question about it. – rid Apr 12 '12 at 21:09
  • @Roland, that's a lot better. – rid Apr 12 '12 at 21:13
  • @Radu - I assumed that this simple part should be understood without me having to write it down, since it's the easiest way to display a certain number of images based on a given number. That's why I haven't asked it like that. – Roland Apr 12 '12 at 21:15
  • 1
    I thought it was an easy straight forward question. Definitely a beginner's question - nonetheless, a valid question. A code sample would have been helpful, but not necessary. I'll help you out @Roland – Thomas Wright Apr 12 '12 at 21:24

2 Answers2

3

you can do an nested for loop like so:

$tranStars;

for($i=0;$i<5;$i++){
  if($i==0){
    for($j=0;$j<$rating;$j++){
      echo "<img src='yourFullStars.png'/>";
      $tranStars=$j;
    }
    $i=$tranStars;
  }
  echo "<img src='yourEmptyStars.png'/>";
}

or you could do two separate for loops:

for($i=0;$i<$rating;$i++){
  echo "<img src='yourFullStars.png'/>";
}
for($i=0;$i<5-$rating;$i++){
  echo "<img src='yourEmptyStars.png'/>";
}

... or any number of things, really.

Thomas Wright
  • 1,309
  • 1
  • 9
  • 15
2

You're already showing 3 filled stars, so all you'd have to do is show the rest unfilled. If you have:

$ratings = 3;                               // 3 filled stars
for ($i = 1; $i <= $ratings; $i++) {        // go through each star
    echo '<img src="filled.png" />';        // show it filled
}

then you would need to find out if you have any more left:

$starsLeft = 5 - $ratings;                  // 2 stars left

and, if so, show the rest as empty stars:

if ($starsLeft > 0) {                       // if there are any more stars left
    for ($i = 1; $i <= $starsLeft; $i++) {  // go through each remaining star
        echo '<img src="empty.png" />';     // show it empty
    }
}
rid
  • 61,078
  • 31
  • 152
  • 193
  • + 1 | I've already used the @Thomas Wright's method, but this works as well. I guess I haven't asked myself how do I display the rest of the stars, I thought of how to do this with forms and an actual rating system. – Roland Apr 12 '12 at 21:32