Eggzorcist Posted November 8, 2010 Share Posted November 8, 2010 I've been thinking of using an assoc_array for validation code in php to have an associative of all the fields (name, email, message) and to see if there has been an "ERROR" declared in any of them? How would I first declare this array? and How would I check to see if they are empty or any of them contain "ERROR". Thanks Quote Link to comment Share on other sites More sharing options...
Anti-Moronic Posted November 8, 2010 Share Posted November 8, 2010 You can define the array like so: $fields = array('name','email','message'); // register error like so: $fields['name']['error'] = 'not enough characters'; // check for error like so: foreach($fields as $field){ if(isset($field['error'])){ // deal with error echo $field['error']; } } ..note, this is VERY basic and can be done a lot better. Ideally you would have a function to manage your fields and errors and they would likely be two separate arrays but this serves a simple purpose. For example, when you register an error, you could register as an array which would allow you to process multiple errors for a single field. If it's not evident how you can develop the above implementation, I'd advise you read up on building and managing multi-dimensional and associative arrays (lot easier than it sounds). Quote Link to comment Share on other sites More sharing options...
Eggzorcist Posted November 8, 2010 Author Share Posted November 8, 2010 Thanks for the push. How is it possible to check the entire array of values to find if there are errors in them? Quote Link to comment Share on other sites More sharing options...
Anti-Moronic Posted November 8, 2010 Share Posted November 8, 2010 Do you mean perform validation for each field? You would do all this within the foreach loop, where you can check for validation and register errors. Quote Link to comment Share on other sites More sharing options...
Eggzorcist Posted November 8, 2010 Author Share Posted November 8, 2010 couldn't I try an in_array() function? Quote Link to comment Share on other sites More sharing options...
Anti-Moronic Posted November 8, 2010 Share Posted November 8, 2010 Yeh, to use in_array you would likely have to have 2 separate arrays. One for the field names, and another for errors. So, with your previous array of $fields..this is $errors: $errors = array(); $errors['name'] = 'incorrect character count'; if(in_array('name',$errors)){ //manage error for 'name' } It would likely be best done using foreach again - but the in_array check would be needless if you managed your field names and errors in the same array. Quote Link to comment Share on other sites More sharing options...
Eggzorcist Posted November 8, 2010 Author Share Posted November 8, 2010 but if I'm looking for each array to send out as a json_encode() and if there is an error it will appear as such $error['message'] = "ERROR" so we can just check $error array, to seek for any "ERROR" in either of ['message'], ['name'], ['email']. Can't I make a really simple if statement using in_array()? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.