如何一次显示错误而不是一次显示错误?

时间:2012-12-09 00:57:59

标签: 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(isset($errors[0])) {
           $errormsg = $errors[0]; 
        } elseif (isset($errors[1])) {
           $errormsg = $errors[1];
        } elseif (isset($errors[1])) {
            $errormsg = $errors[1]; 
        }
    }         

1 个答案:

答案 0 :(得分: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设置的变量,如果在其他地方设置的话;然后忽略这个。否则,我会审核您的代码。

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