PHP根据值返回一定数量的代码

I am trying to create a visual representation of a user rating from 1 to 5 (including .5 values) in the form of stars and half-stars. I have already created the array of user ratings from the MySQL database, I have averaged those values to create an average user rating in the form of a number. What I would like to do is take this value..

$averageuserrating

And if that value is "1", then return this code once

 echo '<img src="images/star.png" class="ratingstar" />';

If $averageuserrating is 1.5, then this code will be returned

<img src="images/star.png" class="ratingstar" /><img src="images/halfstar.png" class="ratingstar" />

And so on... Is there a way to do this without 10 nested if statements?

It can be done by something like this:

$fullStar = '<img src="images/star.png" class="ratingstar" />';
$halfStar = '<img src="images/halfstar.png" class="ratingstar" />';

$rating = 1.5;

while($rating > 0){
    if($rating >= 1){
        echo $fullStar;
        $rating = $rating - 1;
        continue;
    }

    if($rating >= 0.5){
        echo $halfStar;
        $rating = $rating - 0.5;
    }
}

On a side note, this is not the best way to display a rating system.

You could use switch statement.

switch($value){
case "1";
echo '<img src="images/star1.png" class="ratingstar" />';
break; 
case "1";
echo '<img src="images/star2.png" class="ratingstar" />';
break;

so on and so forth.. 

}
for($i = 1; $i <= floor($averageuserrating); $i++)
    echo '<img src="images/star.png" class="ratingstar" />';
if($averageuserrating > floor($averageuserrating))
    echo '<img src="images/halfstar.png" class="ratingstar" />';

Note: If you have averages not only incremented by 0.5, but for example you can get 1.4: Change the second condition to:

if($averageuserrating >= (floor($averageuserrating) + 0.5))

Just use str_repeat function and a bit mathematics.

$full_star = '<img src="images/star.png" class="ratingstar" />';
$half_star = '<img src="images/halfstar.png" class="ratingstar" />';

echo str_repeat( $full_star, floor($averageuserrating) );
if(! is_int($averageuserrating) ){
    echo $half_star;
}

I think it will work fine.

Here is the code for a possible solution:

$numWholeStars = (int)$averageuserrating;

$numHalfStars = $averageuserrating - (int)$averageuserrating;

for($i = 0; $i < $numWholeStars; $i++) {
    echo '<img src="images/star.png" class="ratingstar" />';
}

if($numHalfStars != 0) {
    echo '<img src="images/halfstar.png" class="ratingstar" />';
}

Sounds like you need to re-factor your underlying code to a) gather all the ratings b) show how many stars the rating should display. Firstly see php math - modulus

get_user_ratings($user="bob")
{
    15 - the user ratings should/could be in 1 place (int)users.total_ratings
    // lets say there are 15 records similar to the one below
    jim, 1, bob 
    ...
    mary, 1.5, bob

    // calculate the average of all votes
    ...
    ...
    /* If your rating system offers the chance to rate a user 1.5 stars
     use modulus to check if the rating needs half stars */
    $stars = "";
    if( $averageuserrating % 1 == 0)
    {
        for(
            i=0;
            i<$averageuserrating-1; 
           $i++;$stars .= '<img src="images/star.png" class="ratingstar" />'; 
        )
        return $stars;
    }
    else
    {
        for(
            i=0;
            i<$averageuserrating-1; 
            $i++;$stars .='<img src="images/star.png" class="ratingstar" />' 
        )
        $stars .= '<img src="images/halfstar.png" class="ratingstar" />'; 
        return $stars;
    }

}
$fullstar = '<img src="images/star.png" class="ratingstar" />';
$halfstar = '<img src="images/halfstar.png" class="ratingstar" />';

$value=2.5;//some rating

$number = number_format ( $value, 1 );
$fs = floor ( $number );  //full stars
$hs=(($value*10)%10)/5;   //half stars


while($fs>0)
{
echo $fullstar;
$fs=$fs-1;
}

if($hs==1)
echo $halfstar;