bugzy Posted April 25, 2012 Share Posted April 25, 2012 Ok I am testing foreach in an array as my practice. I wonder how can I assign values in the key of the array.. Here's the code <?php $username = me_mysql_prep(trim($_POST['username'])); $first_name = me_mysql_prep(trim($_POST['first_name'])); $last_name = me_mysql_prep($_POST['last_name']); $email = me_mysql_prep($_POST['email']); $position = me_mysql_prep($_POST['Position']); $password = me_mysql_prep($_POST['password']); $hashed_password = sha1($password); $required_fields = array($username,$first_name,$last_name, $email, $position, $password); foreach($required as $key => $value) { if($value == "") { echo $key . " Cannot be empty<br>"; } } ?> So the result would be like this... IF username, first name and last name are EMPTY it will echo: username cannot be empty first name cannot be empty last name cannot be empty Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/261559-beginner-question-changing-key-values-in-array/ Share on other sites More sharing options...
MMDE Posted April 25, 2012 Share Posted April 25, 2012 $error_messages = array('username cannot be empty', 'first name cannot be empty', 'last name cannot be empty'); $errors = ''; foreach($required as $key => $value){ if(empty($value)){ $errors .= $error_messages[$key].'<br />'; } } echo $errors; I edited to make it possible to print the error messages to the user later in the code. Quote Link to comment https://forums.phpfreaks.com/topic/261559-beginner-question-changing-key-values-in-array/#findComment-1340296 Share on other sites More sharing options...
creata.physics Posted April 25, 2012 Share Posted April 25, 2012 MMDE, why are you setting the error messages as an array? You should explain that he needs to finish alterting the $error_messages array with the rest of the messages or he'll get undefined offset errors. Quote Link to comment https://forums.phpfreaks.com/topic/261559-beginner-question-changing-key-values-in-array/#findComment-1340298 Share on other sites More sharing options...
MMDE Posted April 25, 2012 Share Posted April 25, 2012 MMDE, why are you setting the error messages as an array? You should explain that he needs to finish alterting the $error_messages array with the rest of the messages or he'll get undefined offset errors. They only gave those error messages, I think they understand that they need to add the rest. Why array? Because at least I think it's faster to access the error messages that way than running through a switch or lots of ifs, it's also takes a whole lot of less space. They could also do this, which probably is wiser: $error_messages = array('username', 'first name', 'last name'); $errors = ''; foreach($required as $key => $value){ if(empty($value)){ $errors .= $error_messages[$key].' cannot be empty<br />'; } } echo $errors; If they want to change the value as the title indicates: foreach($required as $key => $value){ if(empty($value)){ $required[$key] = 'new value'; } } Quote Link to comment https://forums.phpfreaks.com/topic/261559-beginner-question-changing-key-values-in-array/#findComment-1340299 Share on other sites More sharing options...
creata.physics Posted April 25, 2012 Share Posted April 25, 2012 Or he can use his original code which works just fine and doesn't need extra complexity for no apparent reason. He just needs to alter his $required_fields array <?php $required_fields = array( 'username' => $username, 'first name' =>$first_name, 'last name' => $last_name, 'email' => $email, 'position' => $position, 'password' => $password ); foreach($required_fields as $key => $value) { if(empty($value)) { echo $key . " Cannot be empty<br>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/261559-beginner-question-changing-key-values-in-array/#findComment-1340301 Share on other sites More sharing options...
bugzy Posted April 25, 2012 Author Share Posted April 25, 2012 Thank you very much guys... Now I'm trying to put all the the "GET" variable from a form to an array to make it more clean and simple code instead of typing each of the variable from the form. I wonder if that is even possible. Quote Link to comment https://forums.phpfreaks.com/topic/261559-beginner-question-changing-key-values-in-array/#findComment-1340302 Share on other sites More sharing options...
MMDE Posted April 25, 2012 Share Posted April 25, 2012 Or he can use his original code which works just fine and doesn't need extra complexity for no apparent reason. He just needs to alter his $required_fields array <?php $required_fields = array( 'username' => $username, 'first name' =>$first_name, 'last name' => $last_name, 'email' => $email, 'position' => $position, 'password' => $password ); foreach($required_fields as $key => $value) { if(empty($value)) { echo $key . " Cannot be empty<br>"; } } I actually wrote some lines where I was thinking they were already set to be like that, but then I realized they were not. Then I stopped thinking that... Yes, you are totally right, they could just do as you say, and I finally understand perfectly well why the title was called what it was. sorry lol, I should probably go to bed, I'm terribly ill today :\ Quote Link to comment https://forums.phpfreaks.com/topic/261559-beginner-question-changing-key-values-in-array/#findComment-1340303 Share on other sites More sharing options...
creata.physics Posted April 25, 2012 Share Posted April 25, 2012 Thank you very much guys... Now I'm trying to put all the the "GET" variable from a form to an array to make it more clean and simple code instead of typing each of the variable from the form. I wonder if that is even possible. What? Any data you want to gather/manipulate from a form will always need to be "typed" manually. Sorry to be a debbie downer. If anybody tells you otherwise don't believe them Also, if your main issue has been resolved please mark this topic as "solved" please. Quote Link to comment https://forums.phpfreaks.com/topic/261559-beginner-question-changing-key-values-in-array/#findComment-1340304 Share on other sites More sharing options...
jcanker Posted April 25, 2012 Share Posted April 25, 2012 Thank you very much guys... Now I'm trying to put all the the "GET" variable from a form to an array to make it more clean and simple code instead of typing each of the variable from the form. I wonder if that is even possible. $_GET is an array-that's why you can access values by key name inside square brackets. foreach($_GET as $key => $value) {...} [\code] Although I should point out that at the beginning of this thread you were using $_POST, not $_GET Quote Link to comment https://forums.phpfreaks.com/topic/261559-beginner-question-changing-key-values-in-array/#findComment-1340306 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.