2012-03-10 4 views
1

내가하는 방법 여기foreach는() 오류 경고 : foreach 문에 대한 공급 잘못된 인수()는

에서 설정 한 오류의 배열이 오류가 계속이

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

내가 계속 코드를입니다 이 "경고 :) (foreach는 대한 공급 잘못된 인수"점점 내가 프로그램 을 실행할 때 내가 왜 못해 완전히 확실 그래서 메신저하지이

$this->errors = array(); 

처럼 생성자에서 오류 배열을 설정을 오류를 인쇄하십시오!

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; 
    } 

여기에 배열 자체에 데이터를 추가하는 방법은 무엇입니까? 도와 주셔서 감사합니다!

괜찮다 제가

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

후 내 페이지에

ERRORS를 출력하는 방법이 추가! 문자열 (20) "잘못된 제출 !!" 내 텍스트 상자에 아무 것도 입력하지 않으면 그 문자열을 말하는가 ??

여기 내 생성자이기도합니다. PHP에 익숙하지 않습니다. 메시지 "invalid submission"가 설정되어있는 양식에 기본 (채워 아무것도) 검증에

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']; 
    } 
+0

더 많은 코드를 게시하십시오. 내 내기는'$ this-> errors'에 추가하려고 시도했지만 실수로'[] '대신'= '를 사용하여 스칼라로 덮어 쓰게됩니다. –

+0

가끔 실수하는 공통점 너무 빨리 입력 할 때'$ this-> errors [] = 'foo';'대신'$ this-> errors = 'foo'; 지금까지이 코드는 문제가 아닙니다. – netcoder

+3

foreach 앞에 발생한 일을 var_dump로 확인하십시오. –

답변

1

, 당신은 오히려 배열에 추가하는 것보다 일반 문자열로 덮어 쓸 수 $this->errors의 원인, 괄호 []을 떠났다.

// Change 
$this->errors = "invalid submission"; 

//...to... 
$this->errors[] = "invalid submission"; 
+0

Michael에 감사드립니다. 예, 그것은 정말로 문제였습니다! – eoin

관련 문제