简体   繁体   English

PHP条件循环帮助

[英]PHP conditional loop help

Hi there in my database I have 3 columns, is_contract, is_permenant and is_temporary. 嗨,在我的数据库中,我有3列is_contract,is_permenant和is_temporary。 Within these columns there is either a Y or N value. 在这些列中,有一个Y或N值。

I am using these columns to echo onto the page what kind of work someone is looking for, my problem is that the user can be looking for more than one type of work, I am currently running 3 if statements to determine what to echo to the page, however I am struggling to add a comma if more than one of the statemnts returns as true, below is my code so far, 我正在使用这些列在页面上回显某人正在寻找的工作,我的问题是用户可能正在寻找一种以上的工作,我目前正在运行3条if语句来确定要回馈给什么的工作页面,但是如果有多个statemnts返回true,则我要添加逗号,以下是到目前为止的代码,

<?php
    if($rslt['is_contract'] == 'Y') {
        echo "Contract ";
}
    if($rslt['is_permanent'] == 'Y') {
        echo "Permanent ";
}
if($rslt['is_temporary'] == 'Y') {
    echo "Temporary";
}
?>
<?php
    $out=array();
    if($rslt['is_contract'] == 'Y') $out[]="Contract";
    if($rslt['is_permanent'] == 'Y') $out[]="Permanent";
    if($rslt['is_temporary'] == 'Y') $out[]="Temporary";
    echo implode(", ",$out);
?>

Either you can use like this in simple way 您可以像这样简单地使用

if($rslt['is_contract'] == 'Y') {
        echo "Contract ";
}
    if($rslt['is_permanent'] == 'Y') {
      if($rslt['is_contract'] == 'Y') {
        echo ", ";
      }
        echo "Permanent ";
      if($rslt['is_temporary'] == 'Y') {
        echo ", ";
      }
}
if($rslt['is_temporary'] == 'Y') {
    if($rslt['is_contract'] == 'Y' && $rslt['is_permanent'] != 'Y') {
        echo ", ";
      }
    echo "Temporary";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM