0

I'm running a two variable dropdown query search but seems to be having a problem with an if-statement not executing when conditions are met. Specifically when the two dropdown list are on the default selected option with a return value of NULL.

Please see code below.

Code for the two dropdown lists:

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>");

If statements. Having problems with the else if (($cs==NULL) && ($sf==NULL)) statement, it is not returning anything.

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 ";

            }
}

UPDATE: Thanks for the comments! I did move the last statement out of the if (isset) condition and into the else statement. However, it is still not working, I found out that instead of going to the else statement when both are NULL. It executes the first if-statement under the condition: (($sf==NULL) && ($cs!==NULL)) the $cs and $sf values are grabbed from the URL: patients.php?cs=&sf=&search=Search. How do I remedy this?

4

1 回答 1

2

你的 if 语句的编写(isset($_GET['cs']) && isset($_GET['sf'])方式必须是真实的,才能得到有问题的 else if 语句。

从逻辑上讲,它们彼此相反,因此您的最终 else 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 ";
}
于 2013-06-05T18:55:24.573 回答