doutang6819 2012-03-10 14:30
浏览 185
已采纳

foreach()错误警告:为foreach()提供的参数无效

i keep getting this error for an array of errors that i have set in a method

here is the code

public function showerrors() {
        echo "<h3> ERRORS!!</h3>";
        foreach ($this->errors as $key => $value)
        {
            echo $value;
        }
    }

i keep getting this "Warning: Invalid argument supplied for foreach()" when i run the program i set the errors array in the constructor like this

 $this->errors = array();

so im not entirely sure why it wont print the errors!

public function validdata() {
        if (!isset($this->email)) {
            $this->errors[] = "email address is empty and is a required field";
        }

        if ($this->password1 !== $this->password2) {
            $this->errors[] = "passwords are not equal ";
        }
        if (!isset($this->password1) || !isset($this->password2)) {
            $this->errors[] = "password fields cannot be empty ";
        }
        if (!isset($this->firstname)) {
            $this->errors[] = "firstname field is empty and is a required field";
        }
        if (!isset($this->secondname)) {
            $this->errors[] = "second name field is empty and is a required field";
        }

        if (!isset($this->city)) {
            $this->errors[] = "city field is empty and is a required field";
        }


        return count($this->errors) ? 0 : 1;
    }

here is how i add data to the array itself! thanks for the help also!

alright i added this to the method

public function showerrors() {
        echo "<h3> ERRORS!!</h3>";
        echo "<p>" . var_dump($this->errors) . "</p>";
        foreach ($this->errors as $key => $value)
        {
            echo $value;
        }

then it outputs on my page this

ERRORS!! string(20) "invalid submission!!" if i type nothing into my text boxes so its saying its a string??

here is my constructor also, soory about this im new to php!

  public function __construct() {

        $this->submit  = isset($_GET['submit'])? 1 : 0;
        $this->errors = array();
        $this->firstname = $this->filter($_GET['firstname']);
        $this->secondname = $this->filter($_GET['surname']);
        $this->email = $this->filter($_GET['email']);
        $this->password1 = $this->filter($_GET['password']);
        $this->password2 = $this->filter($_GET['renter']);
        $this->address1 = $this->filter($_GET['address1']);
        $this->address2 = $this->filter($_GET['address2']);

        $this->city = $this->filter($_GET['city']);
        $this->country = $this->filter($_GET['country']);
        $this->postcode = $this->filter($_GET['postcode']);


        $this->token = $_GET['token'];
    }
  • 写回答

1条回答 默认 最新

  • duanjurong1347 2012-03-10 15:07
    关注

    On your default (nothing filled in) validation to your form, where the message "invalid submission" is set, you left out brackets [], causing $this->errors to be overwritten with a plain string rather than appending to the array.

    // Change
    $this->errors = "invalid submission";
    
    //...to...
    $this->errors[] = "invalid submission";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?