如何使用PHP组合检查空条件?

时间:2018-07-18 07:43:00

标签: php combinations

这里我有5个字段:

  1. clgID
  2. empID
  3. startDate
  4. 限制
  5. papercode

这里所有字段都是必填字段,假设该字段中的任何一个都为空,则表示我必须像clgID should not be emptyempID should not be empty一样返回,据我所知,我编写的代码是{{3} },但我认为我的逻辑不正确。我希望我们必须执行combination逻辑,但是我不知道如何用PHP编写此逻辑。请任何人更新我的代码

我的代码:

//Test Case : 1 
$case1['testCase'] = 'Checking empty condition for all fields';
$case1['clgID'] = '';
$case1['empID'] = '';
$case1['startDate'] = '';
$case1['limit'] = '';
$case1['papercode'] = '';

foreach($mainArray as $key => $val){    
    $input['clgID'] = $val['clgID'];
    $input['empID'] = $val['empID'];
    $input['startDate'] = $val['startDate'];
    $input['limit'] = $val['limit'];
    $input['papercode'] = $val['papercode'];
    // $response = GetResponse($API_URL.'resultTrail', $input);

    if($val['clgID'] != '' && $val['empID'] != '' && $val['startDate'] != '' && $val['limit'] != '' && $val['papercode'] != '' )
    {
        $result[$key]['testCase'] = $val['testCase'];
        $result[$key]['resultCode'] = 'c001';
        $result[$key]['devTeamResult'] = 'success'; 
        $result[$key]['testingTeamResult'] ='test case success';
    }else
    {
        if($val['clgID'] == '' && $val['empID'] == '' && $val['startDate'] == '' && $val['limit'] == '' && $val['papercode'] == '' ){ // Checking empty condition for all fields
            $result[$key]['testCase'] = $val['testCase'];
            $result[$key]['resultCode'] ='458';
            $result[$key]['devTeamResult'] = 'Parameter not matching min requirement'; 
            $result[$key]['testingTeamResult'] ='test case failure';
            $result[$key]['data'] = 'All fields mandatory';
        }else{
            if($val['clgID'] == '' ){ // Checking empty condition for clgID
                $result[$key]['testCase'] = $val['testCase'];
                $result[$key]['resultCode'] ='458';
                $result[$key]['devTeamResult'] = 'Parameter not matching min requirement'; 
                $result[$key]['testingTeamResult'] ='test case success';
                $result[$key]['data'] ='clgID should not be empty';
            }else{
                if($val['empID'] == '' ){ // Checking empty condition for empID
                    $result[$key]['testCase'] = $val['testCase'];
                    $result[$key]['resultCode'] ='458';
                    $result[$key]['devTeamResult'] = 'Parameter not matching min requirement'; 
                    $result[$key]['testingTeamResult'] ='test case success';
                    $result[$key]['data'] = 'empID should not be empty';
                }else{
                    if($val['startDate'] == '' ){ // Checking empty condition for startDate
                        $result[$key]['testCase'] = $val['testCase'];
                        $result[$key]['resultCode'] ='458';
                        $result[$key]['devTeamResult'] = 'Parameter not matching min requirement';  
                        $result[$key]['testingTeamResult'] ='test case success';
                        $result[$key]['data'] =  'startDate should not be empty';
                    }
                } 
            }   
        }   
    }
}

print_r($result);

3 个答案:

答案 0 :(得分:0)

您真的要写出那么多代码吗?如果必须添加另一个必填字段会发生什么情况?

相反,要更加通用。

$requiredFields = ["groupID", "studentID", "startFrom", "limit", "worksheetID"];
foreach($requiredFields as $key) {
    if( empty($input[$key])) {
        return $key." is required"; // or whatever error message
    }
}
// if you get this far, ie. without hitting the "return", then all inputs are present.
// process the data now.

答案 1 :(得分:0)

您可以在每个输入字段中使用此模式:

$var = isset($_POST['field']) ? $_POST['field'] : '';

// Trim any trailing whitespaces
$conditionsOk = true;
if(trim($var) === '') 
{ 
    // Empty input
    $conditionsOk = false;
}

if($conditionsOk) { // Perform action: db update, email... }

答案 2 :(得分:0)

让我们借此机会首先解决此问题,以检查其是否为空。

在php中,isset()函数检查索引是否在数组中以及是否为空。

其次,让我们回顾一下代码并对其进行清理;请查看更改;干净的代码是成为成功的开发人员的最重要因素。

else {
  if (condition) { 
  }
}

重写为:

else if (condition) {
}

这是代码的问题之一,第二个问题是代码的整洁度;始终尝试将嵌套的if语句保持在最低限度,并将功能移至各个功能,以保持代码的可读性和整洁性。 -不可读或不可维护的代码不包含任何值。

我会进行映射并使用动态功能。

映射示例:

$fieldsMapping = [
  "field" => exampleValidationFunction(),
  "field2" => ["required" => True],
]

作为回报,您可以灵活并轻松地维护代码,尽管学习过程很容易。

检查映射的功能可以如下:

function runTestCases() {
  foreach ($fieldsMapping as $field => $validationRules) {
    // your code here - check if its a function.. call function.. etc..
  }
}