Jump to content

Trouble with foreach loop.


bigboss

Recommended Posts

Hi, I am having problems with a foreach loop. It's for a registration system that works by having the form in register.php. The form method of register is test.php, this posts the contents out of the form to this page for validation. Inside test.php there is:

 

include 'config.php'; #Database connection info.
include 'functions/functions.php';
if (isset($_POST['submit'])) { #Form is submitted.

             $lables = array( "username" => "u",
                      "password" => "p",
                      "repassword" => "rp",
                      "email" => "e",
                      "reemail" => "re",					 
                                   "first_name" => "f",
                      "last_name" => "l",
                                   "address1" => "a1",
                                   "address2" => "a2",
                      "towncity" => "t",
                      "postcode" => "po",
	                      "telephone" => "te");

foreach($_POST as $field => $value){
	if($field != "address2" || "telephone"){
		if(empty($value)){
			$blank_fields[] = $field;
		}
	}
} #End of Foreach.

if(sizeof($blank_fields > 0)){
	foreach($blank_fields as $value){
		$current = $lables[$value];
		$errors = makeError($current);
		echo $errors;
		#header("Location: register.php?" . $errors);
	}
}
}

 

This calls  a function that does.

 

function makeError($string){
$error = $string;
if(strlen($string == 1)){
	$error = $string;
} else {
	$error = $string . "&";
}
return $error;	
}

 

I want to use $_GET to send errors back to the register page from the test page, I use the function to generate the indexes so I can use header and then use $_GET with if statements to generate the errors.

 

But if I use the header it will only echo the last value of the array, however if I don't use the header I can echo all of the errors not just the last value of the array. I know this is to do with the foreach loop, but I can't figure out how to fix this.

 

MY second problem is when I echo all of the errors it ends with an ampersand, I don't want this. I have managed to remove this by using.

 

$error = substr($error,0,-1);

 

However there is the same problem as before, because of the foreach loop this will remove all of the & form the error variable, I just want to remove the last instance. I have used other functions built into PHP to do this and they have the same effect.

 

Is there anyway to modify the way I have used the foreach loop for validation that will enable to to send errors back to the register.php using $_GET and remove the final & from the variable that contains the errors.

Link to comment
Share on other sites

Hi

 

Think the issue is that you have the header "call" within the foreach loop, so it will just disappear elsewhere before finishing the loop.

 

include 'config.php'; #Database connection info.
include 'functions/functions.php';
if (isset($_POST['submit'])) { #Form is submitted.

             $lables = array( "username" => "u",
                      "password" => "p",
                      "repassword" => "rp",
                      "email" => "e",
                      "reemail" => "re",					 
                                   "first_name" => "f",
                      "last_name" => "l",
                                   "address1" => "a1",
                                   "address2" => "a2",
                      "towncity" => "t",
                      "postcode" => "po",
	                      "telephone" => "te");

foreach($_POST as $field => $value){
	if($field != "address2" || $field != "telephone"){
		if(empty($value)){
			$blank_fields[] = $field;
		}
	}
} #End of Foreach.

if(sizeof($blank_fields > 0))
{
	foreach($blank_fields as $value)
	{
		$current = $lables[$value];
		$errors = makeError($current);
	}
	#header("Location: register.php?" . $errors);
}
}

 

All the ebest

 

Keith

Link to comment
Share on other sites

To reply to both posts. Thanks for all your help really appreciate it.

 

I want users to be able to leave both of the fields blank so I am sure it is || instead of && because otherwise both of the fields will need to be blank for it to trigger. But Thanks for the help with the syntax.

 

Thanks for the second reply, I did try that and it seemed to have the same effect as having the header within the foreach. I will try it though.

Link to comment
Share on other sites

I want users to be able to leave both of the fields blank so I am sure it is || instead of && because otherwise both of the fields will need to be blank for it to trigger.

 

Take it you noticed that if($field != "address2" || "telephone") will have odd effects. You need a if($field != "address2" || $field != "telephone"). Suspect the first would just evaluate to true every time (ie, it would just think that "telephone" was non zero hence true, and with the or it wouldn't really care about the first part of the if statement).

 

All the best

 

Keith

Link to comment
Share on other sites

Yeah I had noticed that's why I thanked Mark for helping me with my syntax. I did try what you suggested and move the header outside of the loop. As I suspected it doesn't fix the problem. I still only get one error returned, this is because it takes the last value in tr he array from the loop and not the whole value I wanted.

 

Wanted output should be: u&p&rp&e&.. etc

 

If I move the header all I get is. &te&.

 

I'm most likely doing this the wrong way, or there is a way easier way out here to do the same thing.

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.