mysql 如何使用php将数字转换为星星?

pw9qyyiw  于 2023-02-21  发布在  Mysql
关注(0)|答案(7)|浏览(213)

我有一个页面,我需要显示星星系统。转换数字视觉明星形象。我有1.5,2,2.5,..5.0。我有一个代码显示转换数字,但5.0显示6星
遵循这条代码
Converting numbers to visual rating (stars)?

$starNumber = 5.0;

  for ($x = 1; $x <= $starNumber; $x++) {
      echo '<li><i class="fa fa-star"></i></li>';
  }
  if (strpos($starNumber, '.')) {
      echo '<li><i class="fa fa-star-half-o"></i></li>';
      $x++;
  }
  while ($x <= 5) {
      echo '<li><i class="fa fa-star-o"></i></li>';
      $x++;
  }

显示为6星级

6g8kf2rb

6g8kf2rb1#

我的解决方案:

for( $x = 0; $x < 5; $x++ )
{
    if( floor( $starNumber )-$x >= 1 )
    { echo '<li><i class="fa fa-star"></i></li>'; }
    elseif( $starNumber-$x > 0 )
    { echo '<li><i class="fa fa-star-half-o"></i></li>'; }
    else
    { echo '<li><i class="fa fa-star-o"></i></li>'; }
}

**一个

只有一个foor循环,我比较$starNumberfloor值(浮点向下舍入)与当前$x值,以回显全星形;否则,如果未舍入值大于当前$x,则I回显半星;否则(值低于当前$x)I回显空星。

am46iovg

am46iovg2#

在这里我想帮助其他人谁是真正寻找小枝模板解决方案....

{% for x in 0..4 %}
   {% if book_data.averageRating|round(2, 'floor') - x >= 1 %}
        <i class="fa fa-star gold"></i>
        {% elseif book_data.averageRating - x > 0 %}
        <i class="fa fa-star-half-o gold"></i>
          {% else %}
        <i class="fa fa-star-o"></i>
   {% endif %}
{% endfor %}
tzdcorbm

tzdcorbm3#

$starLimit  = 5;
$liStarRate = "";
for($star=1; $star<=$starLimit; $star++) {
    //this holds .5 values
    $liStarRate.='<li star-rate="'.($star-.5).'"><i class="fa fa-star-half-o"></i></li>';
    //this holds the whole values
    $liStarRate.='<li star-rate="$star"><i class="fa fa-star-o"></i></li>';
}

使用上面的简单循环语句,其中不需要任何条件语句,因为每个整数都可以通过简单地减去0.5而变为浮点数。

mefy6pfw

mefy6pfw4#

PHP语言:

function stars($rating){
    for($i = 0; $i < 5; $i++){
        if($i < $rating){
            echo "<i class='fas fa-star glow'></i>";
        } else {
            echo "<i class='fas fa-star'></i>";
        }
    }
}

CSS:

.glow {
   color: yellow;
}

然后像这样使用它:

stars(4);

我使用了FontAwesome v5.5.0。

du7egjpx

du7egjpx5#

我其实只是结束了这样的工作。有点失望地发现,这里的答案似乎只适用于全星(评级1-5),而我想要的东西与半星(评级1-10)
我最终创造了这个:

function starRating($rating){
    //Rating should be from 1-10. This function will produce a 5 star rating with half-stars

    $html = '';

    $full_stars = floor($rating/2);
    //10 = 5, 8 = 4, etc
    $half_star = $rating%2;
    //Should be 1 for every odd number.     
    $empty_stars = 5 - $full_stars - $half_star;

    for($i = 0; $i < $full_stars; $i++){
        //Add all of the full stars
        $html .= '<span class="fa fa-star checked"></span>';
    }

    if($half_star){
        //We can get away with this because 0 == false
        //Add half star         
        $html .= '<span class="fa fa-star-half-alt checked"></span>';
    }

    for($i = 0; $i < $empty_stars; $i++){
        //Fill any remainder with empty stars.
        $html .= '<span class="fa fa-star"></span>';
    }
    return $html;
}

0的输出为5颗空白星。10的输出为5颗整星。5的输出为2颗整星、1颗半星和2颗空白星。

insrf1ej

insrf1ej6#

$starsOutput = '';
$wholeStars = floor($rating);
$halfStars = ($rating - $wholeStars) * 2;
$emptyStars = 5 - ($wholeStars + $halfStars);
for ($i=0; $i < $wholeStars; $i++) { 
    $starsOutput .= '<i class="fas fa-star"></i>';
}
for ($i=0; $i < $halfStars; $i++) { 
    $starsOutput .= '<i class="fas fa-star-half-alt"></i>';
}
for ($i=0; $i < $emptyStars; $i++) { 
    $starsOutput .= '<i class="far fa-star"></i>';
}
ldxq2e6h

ldxq2e6h7#

<?php 

              $id = $_GET['id'];
            $edit = mysqli_query($conn, "SELECT * FROM feedback WHERE id=$id");
            $edit1 = mysqli_fetch_array($edit);
              ?>
<div class="form-group">
                  <label>Rating</label><br>
                  <?php $abc=$edit1['4']; ?> <--value columnnumber of table-->
<fieldset class="rating">
    <input type="radio" id="star5" <?php if($abc==5){echo 'checked';}?>  name="rating" value="<?php echo $abc; ?>" /><label class = "full" for="star5" title="Awesome - 5 stars"></label>
    <input type="radio" id="star4half" <?php if($abc==4.5){echo 'checked';}?> name="rating" value="<?php echo $abc; ?>" /><label class="half" for="star4half" title="Pretty good - 4.5 stars"></label>
    <input type="radio" id="star4" <?php if($abc==4){echo 'checked';}?> name="rating" value="<?php echo $abc; ?>" /><label class = "full" for="star4" title="Pretty good - 4 stars"></label>
    <input type="radio" id="star3half" <?php if($abc==3.5){echo 'checked';}?> name="rating" value="3.5" /><label class="half" for="star3half" title="Meh - 3.5 stars"></label>
    <input type="radio" id="star3" name="rating" <?php if($abc==3){echo 'checked';}?> value="3" /><label class = "full" for="star3" title="Meh - 3 stars"></label>
    <input type="radio" id="star2half" name="rating" <?php if($abc==2.5){echo 'checked';}?> value="2.5" /><label class="half" for="star2half" title="Kinda bad - 2.5 stars"></label>
    <input type="radio" id="star2" name="rating" <?php if($abc==2){echo 'checked';}?> value="2" /><label class = "full" for="star2" title="Kinda bad - 2 stars"></label>
    <input type="radio" id="star1half" name="rating" <?php if($abc==1.5){echo 'checked';}?> value="1.5" /><label class="half" for="star1half" title="Meh - 1.5 stars"></label>
    <input type="radio" id="star1" name="rating" value="1" <?php if($abc==1){echo 'checked';}?> /><label class = "full" for="star1" title="Sucks big time - 1 star"></label>
    <input type="radio" id="starhalf" name="rating" <?php if($abc==.5){echo 'checked';}?> value=".5" /><label class="half" for="starhalf" title="Sucks big time - 0.5 stars"></label>enter code here

</fieldset>

相关问题