2014-01-10 86 views
0

如果数字在80到99之间,我显示的是我的数据库的统计数据如果数字介于51和79之间,我希望标签为标签成功 - 警告,如果数字介于0和50之间,则为标签危险。PHP,Bootstrap - 根据数字范围更改范围标签

事情是这样的:

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

} 

谢谢!

+0

什么你的问题? – Rottingham

+0

这不行,我在问怎么做。 – Koala

+0

你想要比较几个数字? $ row是一个数组。该ID?行总数? –

回答

2

这是一个奇怪的,你循环了一系列的数字,并将数字分配给$行...如果我理解你的愿望,我认为你需要改变你的代码看起来像这样,特别是在每一行上查看$ points值是否在特定范围内。

最新更新增加了一个seperatelabel的点和投票

$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> 
'; 
+0

评论说:“if $ votes or $点数在其中一个范围内“,所以你应该为$ votes添加支票,另外,我会默认为”危险“,而不是”成功“,以防万一 – miyasudokoro

+0

这是我想要的,但有多个变量如$点和$票,我没有显示和多个我的意思是20-30变量,我应该这样做每个? – Koala

+0

@ user1626410是的,但我只是编辑它使用点和票的标签 – Rottingham