bigboss Posted March 6, 2009 Share Posted March 6, 2009 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. Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted March 6, 2009 Share Posted March 6, 2009 Should probably be foreach($_POST as $field => $value){ if($field != "address2" && $field != "telephone"){ if(empty($value)){ $blank_fields[] = $field; } } } #End of Foreach. Quote Link to comment Share on other sites More sharing options...
kickstart Posted March 6, 2009 Share Posted March 6, 2009 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 Quote Link to comment Share on other sites More sharing options...
bigboss Posted March 6, 2009 Author Share Posted March 6, 2009 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. Quote Link to comment Share on other sites More sharing options...
kickstart Posted March 6, 2009 Share Posted March 6, 2009 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 Quote Link to comment Share on other sites More sharing options...
bigboss Posted March 6, 2009 Author Share Posted March 6, 2009 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. Quote Link to comment Share on other sites More sharing options...
kickstart Posted March 6, 2009 Share Posted March 6, 2009 Hi Seen an issue. Change $errors = makeError($current); to $errors .= makeError($current); (not the dot before the =). You probably should initialise $errors to "" at the start of the script. All the best Keith Quote Link to comment Share on other sites More sharing options...
bigboss Posted March 6, 2009 Author Share Posted March 6, 2009 You Sir, are a Gentleman and a Squire, It works!!! Does it now work by adding the value back onto the variable using the dot? Quote Link to comment Share on other sites More sharing options...
kickstart Posted March 6, 2009 Share Posted March 6, 2009 Hi Yep. A "." is a contactenation in php. A ".=" puts a variable / constant onto the end of the existing variable on the left of the expression. So $error .= "Help"; is the same as $error = $error . "Help"; All the best Keith 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.