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?