HDFilmMaker2112 Posted June 1, 2012 Share Posted June 1, 2012 In the past this is how I've done most of my error checking: if(empty($register_account_type) || !isset($register_account_type)){ $error[0]=1; } else{ $error[0]=0; } if(empty($register_fname) || !isset($register_fname)){ $error[1]=1; } else{ $error[1]=0; } if(empty($register_lname) || !isset($register_lname)){ $error[2]=1; } else{ $error[2]=0; } if(empty($register_email) || !isset($register_email)){ $error[3]=1; } else{ $error[3]=0; } if(empty($register_check_email) || !isset($register_check_email)){ $error[4]=1; } else{ $error[4]=0; } if(empty($register_password) || !isset($register_password)){ $error[5]=1; } else{ $error[5]=0; } if(empty($register_check_password) || !isset($register_check_password)){ $error[6]=1; } else{ $error[6]=0; } if(empty($register_gender) || !isset($register_register)){ $error[7]=1; } else{ $error[7]=0; } if(empty($register_month) || !isset($register_month)){ $error[8]=1; } else{ $error[8]=0; } if(empty($register_day) || !isset($register_day)){ $error[9]=1; } else{ $error[9]=0; } if(empty($register_year) || !isset($register_year)){ $error[10]=1; } else{ $error[10]=0; } if(empty($register_membership_type) || !isset($register_membership_type)){ $error[11]=1; } else{ $error[11]=0; } if(empty($register_submit_button) || !isset($register_submit_button)){ $error[12]=1; } else{ $error[12]=0; } if($register_email!=$register_check_email){ $error[13]=1; } else{ $error[13]=0; } if($register_password!=$register_check_password){ $error[14]=1; } else{ $error[14]=0; } if($error!=="000000000000000"){ $_SESSION['error']=$error; header("Location: redirect back to form"); } Would there be some way to simply this to a function or a class? Quote Link to comment Share on other sites More sharing options...
HDFilmMaker2112 Posted June 1, 2012 Author Share Posted June 1, 2012 Basically I want it to function close to the following way. I would generate an array in this format: array_name($variable_name, array_of_type_of_error_checking(Could check for isset, empty, blank($variable_name==""), or match(compare two variables to see if they're the same)), if match variable to compare it to); From there, it would need to pull that data into a function, have it trigger the correct error checking called for and output an array $error with either a 0 or 1 based on the conditions evaluating to true or not. I could use a little help on polishing up how this would actually function, and how to implement it. This is what I have so far as an example for the data input side: $register_account_type_array=array($register_account_type, array("isset","empty","blank")); $register_fname_array=array($register_fname, array("isset","empty","blank")); $register_lname_array=array($register_lane, array("isset","empty","blank")); $register_email_array=array($register_email, array("isset","empty","blank","match"), $register_check_email); $merged_array=array_merge($register_account_type_array, $register_fname_array, $register_lname_array, $register_email_array); $error = error_check($merged_array); I need a way to split the merged_array once it gets into the function. No idea how to do this. It needs to essentially split it back to each individual array before it got merged into one array. And then break off the "error check type assigning" sub-array to trigger their specific error checking job. Quote Link to comment Share on other sites More sharing options...
kicken Posted June 1, 2012 Share Posted June 1, 2012 Checking for empty includes !isset so all your additional isset checks are unnecessary and can be removed. If all those variables match up to keys in $_POST (or similar) then you could write a function which accepts the array and key names to check for. function hasEmptyFields($arr, $keys){ $error=str_repeat('0', count($keys)); foreach ($keys as $i=>$k){ if (empty($arr[$k])){ $error[$i]='1'; } } return $error; } $fields = array('register_account_type', 'register_fname'); //add as many as needed if (0 != (int)hasEmptyFields($_POST, $fields)){ //something is blank. } I coded it to replicate the way your $error variable is generated in case you use it to determine error messages or something. If all you need is a yes/no answer you can change it to just return true or false from the function rather than create that error variable. Quote Link to comment Share on other sites More sharing options...
HDFilmMaker2112 Posted June 1, 2012 Author Share Posted June 1, 2012 Checking for empty includes !isset so all your additional isset checks are unnecessary and can be removed. If all those variables match up to keys in $_POST (or similar) then you could write a function which accepts the array and key names to check for. function hasEmptyFields($arr, $keys){ $error=str_repeat('0', count($keys)); foreach ($keys as $i=>$k){ if (empty($arr[$k])){ $error[$i]='1'; } } return $error; } $fields = array('register_account_type', 'register_fname'); //add as many as needed if (0 != (int)hasEmptyFields($_POST, $fields)){ //something is blank. } I coded it to replicate the way your $error variable is generated in case you use it to determine error messages or something. If all you need is a yes/no answer you can change it to just return true or false from the function rather than create that error variable. That's nearly perfect. Is there any way to add in a variable matching comparison? As per the last two if statements in my original code: if($register_email!=$register_check_email){ $error[13]=1; } else{ $error[13]=0; } if($register_password!=$register_check_password){ $error[14]=1; } else{ $error[14]=0; } Quote Link to comment Share on other sites More sharing options...
HDFilmMaker2112 Posted June 1, 2012 Author Share Posted June 1, 2012 I guess for those few other situations I could just do the checking manually and append to the $error array as follows?: $error() = (int)hasEmptyFields($_POST, $fields) if($register_email!=$register_check_email){ $error[]=1; } else{ $error[]=0; } if($register_password!=$register_check_password){ $error[]=1; } else{ $error[]=0; } $number_of_zeros=str_repeat('0', count($error)); if ($number_of_zeros != $error){ header(Location: ); } Just looking for confirmation that would work. I'm hesitant to test, as I don't want to screw up any other code. 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.