Jump to content

passing an iteration of a loop - my method wont work..... ?


PC Nerd

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.