i'm generating a dynamic form using php. but my problem is how to store each field values into a multidimension array of unknown size.
to be clear:
1. i enter the number of entries i wish to submit
2. php will then generate a single form, but with specified number of set of fields i entered before.
much clearer:
1. how many records you wish to add? i enter 5.
2. php will generate 5 sets of these fields:
-----name:
-----address:
-----position:
what i plan is this:
<?php if(!isset($_POST['noRec']) || (isset($_POST['noRec']) && empty($_POST['noRec'])) || isset($_POST['noRec']) && $_POST['noRec']<0)
{ ?>
<form method="post" action="<?php $_SERVER['PHP_SELF'];?>">
<span>How many employee do you want to add? </span>
<input type="text" size="5" maxlength="2" name="noRec" /><br />
<input type="submit" value="Add Record" />
</form>
<?php }
elseif(isset($_POST['noRec'])) { ?>
<form method="post" action=""> <?php
for($i=0;$i<$_POST['noRec'];$i++) { echo "\n"; ?>
<table>
<tr>
<td class="left">Employee No:</td>
<td><input type="text" name="<?php echo "em[$i][emNo]"; ?>" size="7" maxlength="2" /></td>
</tr>
<tr>
<td class="left">First Name:</td>
<td><input type="text" name="<?php echo "em[$i][fName]"; ?>" /></td>
</tr>
<tr>
<td class="left">Last Name:</td>
<td><input type="text" name="<?php echo "em[$i][lName]"; ?>" /></td>
</tr>
<tr>
<td class="left">Position:</td>
<td><input type="text" name="<?php echo "em[$i][pos]"; ?>" /></td>
</tr>
<tr>
<td class="left">Department:</td>
<td><input type="text" name="<?php echo "em[$i][dept]"; ?>" /></td>
</tr>
</table>
<hr noshade="noshade" />
<?php } echo "\n"; ?>
<input type="submit" />
</form>
<?php
}?>
i intend to group the related records together. say:
$record = array( array('emNo' => '',
'fName' => '',
'lName' => '',
'pos' => '',
'dept' => '' ) );
but i don't know if this is possible. because i want to use emNo, fname, lname, pos, dept as keys.
i want to reference a field value by this:
echo "$record[0]['fName']";
hmm maybe that's all i want to clarify. now what i want to find out are:
1. how to declare/create an array of unknown size? since form fields will be generated dynamically during php runtime
2. how to make a multidimensional array that has an index key and an associative key? eg: $record[0]
please help. THANKS A LOT.
- michael gerona