Check for empty array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mrvanjohnson
Forum Contributor
Posts: 137
Joined: Wed May 28, 2003 11:38 am
Location: San Diego, CA

Check for empty array

Post by mrvanjohnson »

OK hoping someone can help out. I am trying to modify/create a PHP Class for form validation. I pretty much have it working the way I need it to with one minor problem, I keep getting an error message in the PHP Script when all the data is valid. The problem stems from the errorresults array.

First I create a Class page.

Code: Select all

class validate {
	var $errorresults ;
	// Create a quick function to store the error messages in an array
	function validate()
	{
		$this -> $errorresults = array();
	} 
	
	//Adds an error message to the array
	function setError($errormessage)
	{
		$this -> errorresults[] = $errormessage;
	} 
	
	//Returns all the Error in the Array
	function returnError ()
	{
		return $this -> errorresults;
	} 

	
	// Reseting the error list 
	function resetErrorList()
	{
		unset($this -> errorresults);
	}
That's pretty much it for the error array, then I do my check

Code: Select all

// Function to check for Alpha Charaters
	function alphaCheck ($value, $errormessage="Invalid chartecters in this field")
	{
		if (preg_match("/^[a-zA-Z\s]+$/", $value)){
		//	return $value;
			echo $value. ' is good <br>';
		}else{
		$this -> setError ($errormessage);
		}
	} 
} //End of validate class
Now all this seems to work fine. If I have errors they are dropped in an array and I am able to display them. But if there are no error I get this

Warning: Invalid argument supplied for foreach() in /path/to/sample/page.php on line 48

line 48 obviously being the line on the sample page the the foreach is on.
Now on the sample page I am doing this

Code: Select all

foreach($v -> returnError() as $output)
	{
		echo ($output.'<br>'); 
	}
And as long as there is invalid data everything is great but no invalid data and we get the error message.

I have tried encapsulating the foreach loop with a if statement on the sample page but I can't seem to get it to work. Here is an example of what I have tried. Results are in the comment

Code: Select all

method1
if (!isset(${$v -> returnError()})) // Doesn't error out but doesn't display Invalid information either

method2
if (isset(${$v -> returnError()})) //errors out when all the data is valid
	
method3
if (!empty(${$v -> returnError()})) //errors out when all the data is valid
I'm starting to think I should be handling the possibility of a null array on the Classes page and not after I return the array to a test page.
Does any one have any ideas?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

function resetErrorList()
{
unset($this -> errorresults);
}
after unset() there is no variable at all.
Next time you return that member you have an undefinded variable -> warning produced, FALSE returned.
And foreach doesn't respond too well to a boolean passed as parameter ;)
If you want it to be an empty array try

Code: Select all

function resetErrorList()
{
  $this -> errorresults = array();
}
btw: from the names of the methods I'd expect something like

Code: Select all

function setError($errormessage)
   {
      $this -> lastError = $errormessage;
   }
   
   //Returns all the Error in the Array
   function returnError ()
   {
      return $this -> lastError;
   }
isn't it more appendError() and returnErrors() ?
Post Reply