Check for empty array
Posted: Mon Aug 25, 2003 11:36 pm
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.
That's pretty much it for the error array, then I do my check
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
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
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?
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);
}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 classWarning: 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>');
}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 validDoes any one have any ideas?