Jump to content

Beginner Question: Changing Key Values in Array


bugzy

Recommended Posts

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?

Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

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';
}
}

Link to comment
Share on other sites

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>";
}	
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :\

Link to comment
Share on other sites

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  8)

 

Also, if your main issue has been resolved please mark this topic as "solved" please.

Link to comment
Share on other sites

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 

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.