dsuoedtom207012191 2015-12-28 21:27
浏览 52
已采纳

对于这种情况,阵列是否比交换机情况更有效?

I'm displaying star ratings based on input from an API (note: only displaying, not receiving ratings at all). The following code works exactly as I need it to, but it's a very large piece of logic for something that feels like it could be massively simplified.

I read a few other tickets here that suggest that using an array might be more effective than a switch case. But because the logic depends on each case between true within a range of numbers (like more than 2.5 but less than 3) I'm not sure an array would work at all in this case.

Bottom line is this: Can this code be massively simplified somehow?

$stars = 3.5;

switch ($stars) {
  case ($stars > 1 && $stars <= 1.5):
    $star2 = 'dashicons-star-half';
    $star3 = 'dashicons-star-empty';
    $star4 = 'dashicons-star-empty';
    $star5 = 'dashicons-star-empty';
    break;
  case($stars > 1.5 && $stars <= 2):
    $star2 = 'dashicons-star-filled';
    $star3 = 'dashicons-star-empty';
    $star4 = 'dashicons-star-empty';
    $star5 = 'dashicons-star-empty';
    break;
  case($stars > 2 && $stars <= 2.5):
    $star2 = 'dashicons-star-filled';
    $star3 = 'dashicons-star-half';
    $star4 = 'dashicons-star-empty';
    $star5 = 'dashicons-star-empty';
    break;
  case($stars > 2.5 && $stars <= 3):
    $star2 = 'dashicons-star-filled';
    $star3 = 'dashicons-star-filled';
    $star4 = 'dashicons-star-empty';
    $star5 = 'dashicons-star-empty';
    break;
  case($stars > 3 && $stars <= 3.5):
    $star2 = 'dashicons-star-filled';
    $star3 = 'dashicons-star-filled';
    $star4 = 'dashicons-star-half';
    $star5 = 'dashicons-star-empty';
    break;
  case($stars > 3.5 && $stars <= 4):
    $star2 = 'dashicons-star-filled';
    $star3 = 'dashicons-star-filled';
    $star4 = 'dashicons-star-filled';
    $star5 = 'dashicons-star-empty';
    break;
  case($stars > 4 && $stars <= 4.5):
    $star2 = 'dashicons-star-filled';
    $star3 = 'dashicons-star-filled';
    $star4 = 'dashicons-star-filled';
    $star5 = 'dashicons-star-half';
    break;
  case($stars > 4.5):
    $star2 = 'dashicons-star-filled';
    $star3 = 'dashicons-star-filled';
    $star4 = 'dashicons-star-filled';
    $star5 = 'dashicons-star-filled';
    break;
  default:
  $star2 = 'dashicons-star-empty';
  $star3 = 'dashicons-star-empty';
  $star4 = 'dashicons-star-empty';
  $star5 = 'dashicons-star-empty';
}

?>
 <div class="wporg-ratings" title="<?php echo $stars; ?> out of 5 stars" style="color:#e6b800;">
   <span class="dashicons dashicons-star-filled"></span>
   <span class="dashicons <?php echo $star2; ?>"></span>
   <span class="dashicons <?php echo $star3; ?>"></span>
   <span class="dashicons <?php echo $star4; ?>"></span>
   <span class="dashicons <?php echo $star5; ?>"></span>
 </div>
  • 写回答

3条回答 默认 最新

  • dongqianwei6664 2015-12-28 21:59
    关注

    I don't think an array or a switch are needed. You can use a for loop, and check the $stars value against your loop variable to see which icon should be used.

    <div class="wporg-ratings" title="<?php echo $stars; ?> out of 5 stars" style="color:#e6b800;">
    <?php
    // for loop: one iteration for each of five possible stars
    // (for loops are generally best for repeating code a specific number of times)
    for ($i=0; $i < 5; $i++) {
        if ($stars <= $i ) {
            // empty stars are displayed when the iterator (i) is >= the star value
            echo  '<span class="dashicons dashicons-star-empty"></span>';
        } elseif ($stars <= $i + 0.5) {
            // half stars are displayed for star values between i and i+0.5 
            echo  '<span class="dashicons dashicons-star-half"></span>';
        } else {
            // whole stars are displayed when the star value is > i+0.5
            echo  '<span class="dashicons dashicons-star-filled"></span>';
        }
    }
    ?>
    </div>
    

    To help understand why this works, take a theoretical value through the loop. We can use the one from your question, 3.5.

    • first iteration: 3.5 > 0, 3.5 > 0.5, so you get the else value (filled star)
    • second iteration: 3.5 > 1, 3.5 > 1.5, so you get the else value (filled star)
    • third iteration: 3.5 > 2, 3.5 > 2.5, so you get the else value (filled star)
    • fourth iteration: 3.5 > 3, 3.5 = 3.5, so you get the elseif value (half star)
    • fifth iteration: 3.5 < 4, so you get the if value (empty star)
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法