douchuose2514 2014-01-10 18:44
浏览 39
已采纳

PHP,Bootstrap - 根据数字范围更改范围标签

I am showing statistics from my database, if the number is between 80 and 99 I want the label to be label-success, if the number is between 51 and 79 label-warning and if the number is between 0 and 50 label-danger.

Something like this:

    $query = $db->query("SELECT * FROM stats");
    foreach ($query as $row) {

                $points = $row['points'];
            $votes = $row['votes'];


$postclass = "";

foreach (range(0,50) as $row) {
    $posclass = "danger";
}

foreach (range(51,79) as $row) {
    $posclass = "warning";
}

foreach (range(80,99) as $row) {
    $posclass = "success";
}


echo '
<span class="label label-'.$posclass.'" >'.$points.'</span>
<span class="label label-'.$posclass.'" >'.$votes.'</span>
';

}

Thanks!

  • 写回答

1条回答 默认 最新

  • duanfan8360 2014-01-10 18:52
    关注

    This is busted an bizarre, you are looping over a range of numbers and assigning the number to $row... If I'm understanding your desire, I think you need to change your code to look like this, specifically on each row to see if the $points value is within a particular range.

    Last update adds a seperatelabel for Points and Votes

    $postclass = "";
    if ($points > 0 && $points <= 50) {
        $pointsLabel = "danger";
    } else if ($points > 50 && $points <= 79) {
        $pointsLabel = "warning";
    } else {
        $pointsLabel = "success";
    }
    
    if ($votes > 0 && $votes<= 50) {
        $votesLabel = "danger";
    } else if ($votes> 50 && $votes<= 79) {
        $votesLabel = "warning";
    } else {
        $votesLabel = "success";
    }
    
    echo '
    <span class="label label-'.$pointsLabel.'" >'.$points.'</span>
    <span class="label label-'.$votesLabel.'" >'.$votes.'</span>
    ';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?