【问题标题】:how to convert number into star rating using php?如何使用php将数字转换为星级?
【发布时间】:2016-02-08 15:01:33
【问题描述】:

我有一个页面需要显示星级评分系统。将数字转换为视觉星形图像。我有 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 颗星

【问题讨论】:

  • if (strpos($starNumber,'.') && $starNumber!=5.0 ) {
  • 你确定吗,因为我已经尝试了上面的代码并且它工作正常。它显示您想要的 5 颗星。我认为问题不在于您发布的代码,而在于您的其余代码。
  • 你检查了 5.0 评级我得到 6 星
  • 是的,和你的代码一样
  • 您必须将while($x&lt;=5) 替换为while($x&lt;5)...

标签: javascript php jquery mysql


【解决方案1】:

在这里,我想帮助其他真正在寻找树枝模板解决方案的人......

{% 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 %}

【讨论】:

    【解决方案2】:

    我的解决方案:

    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>'; }
    }
    

    PhpFiddle demo

    只有一个foor 循环,我将$starNumberfloor 值(向下四舍五入)与当前$x 值进行比较以回显全星;否则如果不取整值大于当前$x我回显半星;否则(值低于当前$x)我回显空星。

    【讨论】:

    • 这是我找到的最简单的解决方案。谢谢!
    • 刚刚从第二个 echo 行中删除了 -o,然后像专业人士一样工作
    【解决方案3】:
    <?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>
    

    【讨论】:

      【解决方案4】:
      $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 来更改为浮点数。

      【讨论】:

        【解决方案5】:

        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。

        【讨论】:

          【解决方案6】:

          我实际上刚刚完成了类似的工作。有点失望地发现这里的答案似乎只适用于满星(评级 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 颗空白星。

          【讨论】:

            【解决方案7】:
            $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>';
            }
            

            【讨论】:

            • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-03-31
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多