-3

在下面的代码中,它假设包含一个数组中的所有错误,然后根据激活的错误显示错误。但问题是它仍然一次只显示一个错误,而不是一次显示相关错误。为了做到这一点,我需要改变什么?

$errors = array();


          if (!$getcourseid){
              $errors[] = "You must enter in Course's ID";
      }else if (!$getcoursename){
          $errors[] = "You must enter in Course's Name";
      }else if (!$getduration){
              $errors[] = "You must select Course's Duration";
          }     



    if(empty($errors)) {
        if ($numrows == 1){
       $errormsg = "<span style='color: green'>Course " . $getcourseid .  " - "  . $getcoursename . " has been Created</span>";
       $getcourseid = "";
       $getcoursename = "";
       $getduration = "";
    }else{
        $errormsg = "An error has occured, Course has not been Created";
    }
    } else {
        if(isset($errors[0])) {
           $errormsg = $errors[0]; 
        } elseif (isset($errors[1])) {
           $errormsg = $errors[1];
        } elseif (isset($errors[1])) {
            $errormsg = $errors[1]; 
        }
    }         
4

1 回答 1

0

尝试这样的解决方案:

    <?php 

$errors = array();


          if (!$getcourseid){
              $errors[] = "You must enter in Course's ID";
      }else if (!$getcoursename){
          $errors[] = "You must enter in Course's Name";
      }else if (!$getduration){
              $errors[] = "You must select Course's Duration";
          }     



    if(empty($errors)) {
        if ($numrows == 1){
       $errormsg = "<span style='color: green'>Course " . $getcourseid .  " - "  . $getcoursename . " has been Created</span>";
       $getcourseid = "";
       $getcoursename = "";
       $getduration = "";
    }else{
        $errormsg = "An error has occured, Course has not been Created";
    }
    } else {
        if (count($errors) > 0)
        {
            foreach ($errors AS $Errors)
            {
                echo "{$Errors} <br>"; 
            }
        }
    }     
?>

如果在其他地方设置了 $numrows,我没有看到为 $numrows 设置的变量;然后忽略这个。否则,我会查看您的代码。

我还会对您的代码演示提出一些建议

于 2012-12-09T01:14:05.300 回答