首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多个if语句工作,除非值为null。

多个if语句工作,除非值为null。
EN

Stack Overflow用户
提问于 2011-10-20 02:23:04
回答 1查看 130关注 0票数 0

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

我有一个mysql数据库,里面满是健身测试结果,网站上有一个页面,显示登录人员的结果。

我们的设备收集的具体数据称为平衡对称,其结果要么是正数,要么是负数。如果一个人有-15%对称或更少,他们需要额外的工作在他们的左腿,如果他们是+15%对称或更少,他们需要更多的工作,他们的右腿。

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

我基本上希望代码可以这样做:

代码语言:javascript
复制
- 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

它与下面代码的工作方式是

代码语言:javascript
复制
- 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

代码语言:javascript
复制
<?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>";
}

?>

谢谢你的帮助!

EN

回答 1

Stack Overflow用户

发布于 2011-10-20 03:37:46

我不知道为什么,但出于某种原因,null被认为不到-15。我认为null在数值比较中会被视为0,但显然不是。您可以很容易地修复这个问题:

代码语言:javascript
复制
else if (!is_null($row['field']) && $row['field'] <= -15) {

您还可以在查询中执行以下检查:

代码语言:javascript
复制
COALESCE(field_symmetry_value, 0)

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7830449

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档