Welcome Guest, Not a member yet? Register   Sign In
Inserting with arrays > A Better Way?
#2

[eluser]coolfactor[/eluser]
PHP has a way to automatically create arrays from submitted form data.

Code:
<input type="text" name="info[name]" value="George" />
<input type="text" name="info[age]" value="32" />
<input type="text" name="info[phone]" value="222-2222" />

Submitting those 3 fields automatically creates an "info" sub-array within your $_POST array.

Code:
$_POST['info'] = array('name' => 'George', 'age' => '32', 'phone' => '222-2222');

So, you could pre-define your array, and then use array_merge to overlay the submitted form data.

Code:
// predefine array
$info = array('name' => '',
              'age' => '',
              'phone' => '');
// overlay $_POST
if (isset($_POST['info'])) {
    $info = array_merge($info, $_POST['info']);
}

Now $info contains everything in the predefined array, plus everything that was submitted. This ensures the fields you need for any database queries are present.


Messages In This Thread
Inserting with arrays > A Better Way? - by El Forum - 08-12-2007, 04:44 PM
Inserting with arrays > A Better Way? - by El Forum - 08-12-2007, 04:53 PM
Inserting with arrays > A Better Way? - by El Forum - 08-12-2007, 05:15 PM
Inserting with arrays > A Better Way? - by El Forum - 08-12-2007, 05:30 PM



Theme © iAndrew 2016 - Forum software by © MyBB