Jump to content

Find value in array and make sure it's set to true, if not make it true.


iarp

Recommended Posts

		
$looponce = 1;
foreach ($this->info as $k => $v) {
if ( (($k == 'subject') && ($v['required'])) && (!$this->settings['customSubject'])) {
	for ($i = 0; $i <= 0; $i++) {
		$output[] = $this->display_errors('error_system_subject');
	}
}
if ( (($k == 'name') && (!$v['required'])) || ((!array_key_exists("name", $this->info)) && ($looponce == 1)) ) {
	$output[] = $this->display_errors('error_system_name');
	$looponce++;
}
}

That is my loop that i check things in. What i'm more interested in is the name if statement.

 

If currently checks for a name key in an array(below) and makes sure that it is set to required. If it's not set to required, print out a system error and die()'s.

 

I'm looking for ways to remove the need for program errors and just change them to what is needed to run.

 

What i know how to do is, get into the inner array, what i don't know is how to edit the data and put it back exactly as it was given to me. The inner array data can be put back in any order, but the outer array must be in exact order as it was given.

 

above code $this->info = $formdata;

$formdata = array(
'name' => array('name'=>"Full Name", 'required'=>false, 'type'=>'text'), # This needs to be required=>true, but i can't trust the user, which is why i have the error.
'telephone' => array('name'=>"Telephone", 'required'=>false, 'type'=>'phone'),
);

 

Any help is greatly appreciated, also am i doing the foreach loop in the code above in an efficient manner or is there another way?

Link to comment
Share on other sites

The reasoning behind what you want to do seems detremental towards your projects consistency.

 

when you program PHP you want to make clear boundaries, arguments must of a specific type, the more strict you are the easier it wil be to maintain the code when it grows. You should keep the error, since it looks like it would be a developer error rather than a user error, it should be a developer error message.

 

For what you want to do though is a slightly more simple loop technique;

$Form_Data = $this->info();
foreach($Form_Data As $key => $val){
// Each time this loops you get one of the form fields. So we can check each form field easily now.
$field = $val;
if($field['required'] !== true){
	// This field says its value is not required.
	// You can do a check here like, 
	if($field['name'] == "Full Name"){ // (i would use strtolower on both variables to not worry about Case)
		// This i think is what your looking for;
		$Form_Data[$key]['required'] = true; // Change the value in the original array.
	}
}
}
// check the array.
print_r($Form_Data);

 

I would hard-code required fields into your script, you could use a mysql table to store which field names are required but for now i think your best to learn by using hard-coded values, you could still use an array, though.

 

eg;

$required_fields = array("name","address","email");
if(in_array($key,$required_fields)){
// ... do the change
}

 

Hope this helps.

Link to comment
Share on other sites

You've made me have a "ah your an idiot iarp" moment. For some reason, in my head i was thinking in order to edit items in an array you needed to take the data out and then put it back in with some kinda merge. But just setting it to true will do exactly what i want.

 

The code is part of a contact form i was asked to build some weeks ago, i did build it for them and it works just fine, but after that i had no work to be done and rather then waste time in games, i started building onto what i had done already.

 

As for the error, yes it is a developer error and i could/should just leave it in. I built another script that puts the formdata array together for people, but everything about the array is editable by the developer in the config file, and TBH what good is a contact form without the users email address?

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.