gpt4 book ai didi

php - 满足条件时 if 语句不执行(条件 : two variable are NULL)

转载 作者:行者123 更新时间:2023-11-29 08:29:55 26 4
gpt4 key购买 nike

我正在运行两个变量下拉查询搜索,但似乎遇到了满足条件时 if 语句不执行的问题。特别是当两个下拉列表位于默认选定选项且返回值为 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)) $cs 和 $sf 值从 URL 中获取:患者.php?cs=&sf=&search=搜索。我该如何解决这个问题?

最佳答案

编写 if 语句的方式 (isset($_GET['cs']) && isset($_GET['sf']) 必须为 true 才能到达 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 ";
}

关于php - 满足条件时 if 语句不执行(条件 : two variable are NULL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16947555/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com