除非值为null,否则多个if语句都有效

时间:2011-10-20 02:23:05

标签: php mysql if-statement

没有做过很多php / mysql,但这似乎应该是一个简单的修复我可能只是搞砸了一些小东西。很抱歉,想要彻底解释的长篇解释

我有一个充满健康测试结果的mysql数据库,网站上有一个显示登录结果的页面。

我们的设备收集了一些称为平衡对称的特定数据,结果是正或负百分比。如果一个人的对称性为-15%或更低,他们的左腿需要额外的工作,如果他们的对称性为+ 15%或更少,他们的右腿需要更多的工作。

这是一个非常重要的因素,所以我试图通过页面顶部的“红旗”告诉他们。数据以负数或正数存储在数据库中。

我基本上希望代码执行此操作:

- if the data is >= 15 then it spits out that they need more work on their right leg
- if the data is <= -15 then it spits out that they need more work on their left leg
- if the data is anything else, so between -14 and 14, or if it is null (sometimes they may not have done the balance part of the fitness test due to injury) then nothing is displayed

它使用以下代码的方式是

- if >= 15 works perfectly tells them to work on their right leg
- if <= -15 works perfectly tells them to work on their left leg
- if between -14 and 14 works perfectly tells them nothing
- if null tells them to work on their left leg for some reason.  how do I make it so that the null value is handled the same way as the values between -14 and 14

PHP

<?php

if ($row['field_symmetry_value'] >= 15){
echo "              <div class='train_row1'>
           <div class='train_row_inside'>
            <div class='train_column1rf'><img src='http://racebattlerecover.com/images/redflag.png' /></div>
            <div class='train_column3rf'><h3>IMPORTANT: Your balance symmetry between your left and right leg is off by more than 15%. You need to add extra balance exercises to your right leg.</h3></div>
           </div>
          </div>";
}
elseif ($row['field_symmetry_value'] <= -15){
echo "              <div class='train_row1'>
           <div class='train_row_inside'>
            <div class='train_column1rf'><img src='http://racebattlerecover.com/images/redflag.png' /></div>
            <div class='train_column3rf'><h3>IMPORTANT: Your balance symmetry between your left and right leg is off by more than 15%. You need to add extra balance exercises to your left leg.</h3></div>
           </div>
          </div>";
}

?>

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

我不确定原因,但出于某种原因,null被认为低于-15。我认为null在数字比较中会被视为0,但显然不是。你可以很容易地解决这个问题:

else if (!is_null($row['field']) && $row['field'] <= -15) {

您也可以在查询中执行此操作:

COALESCE(field_symmetry_value, 0)

如果field_symmetry_valueNULL,则会返回0,不会打印任何内容。