如果语句在满足条件时不执行(条件:两个变量为NULL)

时间:2013-06-05 18:47:51

标签: php html mysql

我正在运行两个变量下拉查询搜索,但似乎遇到if-statement在满足条件时不执行的问题。特别是当两个下拉列表位于默认选定选项上时,返回值为NULL

请参阅以下代码。

两个下拉列表的代码:

print("<form action='patients.php' method='get'>");



// csSelection & sfSelection declaration of arrays to populate dropdown selection
$csSelection=array("","","","","");
$sfSelection=array("","","","","");
if(isset($_REQUEST['search'])) {

    if (isset($_GET['cs'])) {
            $csSelection=array("","","","","");
            $cs= $_GET['cs'];

            if ($cs=='1') {$csSelection[1]='selected';}
            else if ($cs=='2') {$csSelection[2]='selected';}
            else if ($cs=='3') {$csSelection[3]='selected';}
            else if ($cs=='4') {$csSelection[4]='selected';}
            else if ($cs=='0') {$csSelection[0]='selected';}
            //var_dump($cs);
            //var_dump($csSelection);
    }

    if (isset($_GET['sf'])) {
            $sfSelection=array("","","","","");
            $sf = $_GET['sf'];

            if ($sf=='Analyzed') {$sfSelection[1]='selected';}
            else if ($sf=='New Case') {$sfSelection[2]='selected';}
            else if ($sf=='Updated Case') {$sfSelection[3]='selected';}
            else if ($sf=='None') {$sfSelection[4]='selected';}
            else if ($sf=='0') {$sfSelection[0]='selected';}
    }
}


// Case status dropdown list
print("<select name='cs'> 
            <option ".$csSelection[0]." value=''>--Select Case Status--</option>
            <option value='1' ".$csSelection[1].">Pass</option>
            <option value='2' ".$csSelection[2].">Failed</option>
            <option value='3' ".$csSelection[3].">Pass With Error</option>
            <option value='4' ".$csSelection[4].">Indeterminate</option>
      </select>");

// Case Flag dropdown list
print("<select name='sf'>
            <option ".$sfSelection[0]." value=''>--Select Flagged Status--</option>
            <option value='Analyzed' ".$sfSelection[1].">Analyzed Case</option>
            <option value='New Case' ".$sfSelection[2].">New Case</option>
            <option value='Updated Case' ".$sfSelection[3].">Updated Case (New Images)</option>
            <option value='None' ".$sfSelection[4].">No Status Flag</option>
      </select>");

print("<input type='submit' name='search' value='Search'></form>");

如果声明。对else if (($cs==NULL) && ($sf==NULL))语句有问题,它没有返回任何内容。

if (isset($_GET['cs']) && isset($_GET['sf']) ) {
            $cs= $_GET['cs'];
            $sf= $_GET['sf'];

            var_dump($cs);
            var_dump($sf);

            if (($sf==NULL) && ($cs!==NULL)) {
                    $sql = "SELECT patientid, sub_status_lookup.CASE_STATUS, sub_status_lookup.SUB_STATUS, count(DISTINCT caseid) as cases_count, cases.comments, date_mod, caseid, user_mod, status_flag  FROM cases, sub_status_lookup
                    Where cases.status = sub_status_lookup.SUB_ID and sub_status_lookup.STATUS_ID = '".$cs."'
                    Group By patientid, cases.status";      

            } else if (($sf!==NULL) && ($cs==NULL)) {
                    $sql = "SELECT patientid, sub_status_lookup.CASE_STATUS, sub_status_lookup.SUB_STATUS, count(DISTINCT caseid) as cases_count, cases.comments, date_mod, caseid, user_mod, status_flag  FROM cases, sub_status_lookup
                    Where cases.status = sub_status_lookup.SUB_ID and cases.status_flag = '".$sf."'
                    Group By patientid, cases.status";      
                    //var_dump($sql);
            } else if (($cs!==NULL) && ($sf!==NULL)){

                    $sql = "SELECT patientid, sub_status_lookup.CASE_STATUS, sub_status_lookup.SUB_STATUS, count(DISTINCT caseid) as cases_count, cases.comments, date_mod, caseid, user_mod, status_flag  FROM cases, sub_status_lookup
                    Where cases.status = sub_status_lookup.SUB_ID and sub_status_lookup.STATUS_ID = '".$cs."' and cases.status_flag = '".$sf."'
                    Group By patientid, cases.status";      

            } else if (($cs==NULL) && ($sf==NULL)) {
                    var_dump($sql); // This does not print out when both $cs and $sf are on the default NULL

                    $sql = "SELECT patientid, sub_status_lookup.CASE_STATUS, sub_status_lookup.SUB_STATUS, count(caseid) as cases_count, cases.comments, date_mod, caseid, user_mod, status_flag  FROM cases, sub_status_lookup
                    Where cases.status = sub_status_lookup.SUB_ID
                    Group By patientid, cases.status ";

            }
}

更新:感谢您的评论!我确实将最后一个语句从if(isset)条件移到了else语句中。但是,它仍然无法正常工作,我发现当两者都为NULL时,而不是转到else语句。它在以下条件下执行第一个if语句:(($sf==NULL) && ($cs!==NULL))从URL中获取$ cs和$ sf值:patients.php?cs =&amp; sf =&amp; search = Search。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

创建if语句的方式(isset($_GET['cs']) && isset($_GET['sf'])必须是真的,甚至可以使用else if语句。

从逻辑上讲,它们是彼此相反的,所以你的最后一个if语句永远不会运行。尝试将$cf == null && $sf == null分支的内容移动到外部if的else子句。

if (isset($_GET['cs']) && isset($_GET['sf']) ) {
            $cs= $_GET['cs'];
            $sf= $_GET['sf'];

            var_dump($cs);
            var_dump($sf);

            if (($sf==NULL) && ($cs!==NULL)) {
                    $sql = "SELECT patientid, sub_status_lookup.CASE_STATUS, sub_status_lookup.SUB_STATUS, count(DISTINCT caseid) as cases_count, cases.comments, date_mod, caseid, user_mod, status_flag  FROM cases, sub_status_lookup
                    Where cases.status = sub_status_lookup.SUB_ID and sub_status_lookup.STATUS_ID = '".$cs."'
                    Group By patientid, cases.status";      

            } else if (($sf!==NULL) && ($cs==NULL)) {
                    $sql = "SELECT patientid, sub_status_lookup.CASE_STATUS, sub_status_lookup.SUB_STATUS, count(DISTINCT caseid) as cases_count, cases.comments, date_mod, caseid, user_mod, status_flag  FROM cases, sub_status_lookup
                    Where cases.status = sub_status_lookup.SUB_ID and cases.status_flag = '".$sf."'
                    Group By patientid, cases.status";      
                    //var_dump($sql);
            } else if (($cs!==NULL) && ($sf!==NULL)){

                    $sql = "SELECT patientid, sub_status_lookup.CASE_STATUS, sub_status_lookup.SUB_STATUS, count(DISTINCT caseid) as cases_count, cases.comments, date_mod, caseid, user_mod, status_flag  FROM cases, sub_status_lookup
                    Where cases.status = sub_status_lookup.SUB_ID and sub_status_lookup.STATUS_ID = '".$cs."' and cases.status_flag = '".$sf."'
                    Group By patientid, cases.status";      

            } 
} else {
    var_dump($sql); // This does not print out when both $cs and $sf are on the default NULL

    $sql = "SELECT patientid, sub_status_lookup.CASE_STATUS, sub_status_lookup.SUB_STATUS, count(caseid) as cases_count, cases.comments, date_mod, caseid, user_mod, status_flag  FROM cases, sub_status_lookup
    Where cases.status = sub_status_lookup.SUB_ID
    Group By patientid, cases.status ";
}