PC Nerd Posted January 2, 2008 Share Posted January 2, 2008 Hi, im using a foreach($_POST as $field => $value) {} to loop through my form input and validate it. I have a line that checks to see if the field is empty, and if it is it sets the error and then passes that iteration: if(empty($value)) {pass;} however it seems not to be doing this. HEre is my code: foreach($_POST as $field => $value) { if($field == 1) {pass;} if($_POST['Save'] == 1 && $field == "User_Name" || $field == "Password" || $field == "Conf_Password" || $field == "Emg_Relation") { ### Validate the Username, password and email if(empty($value)) { $ERROR[$idx]['err'] = "empty"; $ERROR[$idx]['empty'] = $field; $idx ++; } else if(!preg_match("[A-za-z]", $value)) { $ERROR[$idx]['err'] = "invalid"; $ERROR[$idx]['empty'] = $field; $idx ++; } else if(strlen($value) > 20 || strlen($value) < 5) { $ERROR[$idx]['len'] = "invalid"; $ERROR[$idx]['empty'] = $field; $idx ++; } else if($field == "Password" && $value != $_POST['Conf_Password']) { $ERROR[$idx]['err'] = "passmtch"; $idx ++; } } if($_POST['Save'] == 0 && empty($value)) { $ERROR[$idx]['err'] = "empty"; $ERROR[$idx]['empty'] = $field; $idx ++; } if($field == "F_Name" || $field == "L_Name" || $field == "User_Name" || $field == "Emg_Name") { ### Validate the names if(!ereg("[A-za-z' -]", $value)) { $ERROR[$idx]['err'] = "invalid"; $ERROR[$idx]['empty'] = $field; $idx ++; } else if(strlen($value) > 20) { $ERROR[$idx]['err'] = "len"; $ERROR[$idx]['empty'] = $field; $idx ++; } ..... i know that my ERROR array is containing one entry for an invalid F_Name, and then an empty error for the F_Name: Here is my error array output below using print_r($ERROR); Array ( [0] => Array ( [err] => empty [empty] => F_Name ) [1] => Array ( [err] => invalid [empty] => F_Name ) [2] => Array ( [err] => invalid [empty] => 1 ) [3] => Array ( [err] => empty [empty] => L_Name ) [4] => Array ( [err] => invalid [empty] => L_Name ) ..... note that the: [empty] => 1 is simply an error which im in the middle of fixing - however it shouldnt effect this outcome Am i using teh correct method to skip th eloop iteration - or is there another way to do it? Thanks for your help in advance Quote Link to comment https://forums.phpfreaks.com/topic/84092-passing-an-iteration-of-a-loop-my-method-wont-work/ Share on other sites More sharing options...
MadTechie Posted January 2, 2008 Share Posted January 2, 2008 I'm a little confused, whats if($field == 1) {pass;} meant to do ? Quote Link to comment https://forums.phpfreaks.com/topic/84092-passing-an-iteration-of-a-loop-my-method-wont-work/#findComment-428044 Share on other sites More sharing options...
PC Nerd Posted January 2, 2008 Author Share Posted January 2, 2008 that was somethign i was trying to do to iliminate the extra value i originally thought that the $_POST array was sending the data twice, uising the string name - and then 1..... which i thought was very wierd - i was just playing with that. Apart from that - im mainly lookingat a a was to miss an iteration of a loop. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/84092-passing-an-iteration-of-a-loop-my-method-wont-work/#findComment-428045 Share on other sites More sharing options...
MadTechie Posted January 2, 2008 Share Posted January 2, 2008 if you wish to skip it, do if($field == 1) {continue;} or i suspect this if($field == "1") {continue;} Quote Link to comment https://forums.phpfreaks.com/topic/84092-passing-an-iteration-of-a-loop-my-method-wont-work/#findComment-428047 Share on other sites More sharing options...
PC Nerd Posted January 2, 2008 Author Share Posted January 2, 2008 ok - I was under teh impression that continue simply continues with the rest of the loop. i want to stop the code for the res of that loop. if it im looping through an array of numbers, to use in a isPrime() function..... eg loop: isPrime()??? if it is a prime, then break this iteration other code here then that would output: other code othercode "PRIME" other code "PRIME" etc. if you get my drift. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/84092-passing-an-iteration-of-a-loop-my-method-wont-work/#findComment-428053 Share on other sites More sharing options...
MadTechie Posted January 2, 2008 Share Posted January 2, 2008 Ok use Break (i'm still confused what your trying to do) Continue SKIPS the rest of the iteration which is what i think your trying to do! continue is used within looping structures to skip the rest of the current loop iteration and continue execution at the condition evaluation and then the beginning of the next iteration. break ends execution of the current for, foreach, while, do-while or switch structure. Quote Link to comment https://forums.phpfreaks.com/topic/84092-passing-an-iteration-of-a-loop-my-method-wont-work/#findComment-428058 Share on other sites More sharing options...
PC Nerd Posted January 2, 2008 Author Share Posted January 2, 2008 ok - thanks Quote Link to comment https://forums.phpfreaks.com/topic/84092-passing-an-iteration-of-a-loop-my-method-wont-work/#findComment-428516 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.