What your probably after is something like the below.
<?php
// form action
$strAction = $_SERVER['PHP_SELF'];
// base $_POST key for users
$strBaseKey = 'raw_new_memeber';
// post fixes for numbers
$arrPostFix = array('st','nd','rd','th','th','th','th','th','th');
// numbers of users
$intUsers = 3;
// form configuration
$arrConfigs = array(
'email'=>array(
'label'=>'Enter E-mail Address'
,'max'=>60
,'required'=>'Y'
)
,'pass'=>array(
'label'=>'Create a Password'
,'max'=>60
,'required'=>'Y'
)
,'user'=>array(
'label'=>'Create a Username'
,'max'=>60
,'required'=>'Y'
)
,'email_copy'=>array(
'label'=>'Retype E-mail Address'
,'max'=>60
,'required'=>'Y'
)
,'pass_copy'=>array(
'label'=>'Re-enter a Password'
,'max'=>60
,'required'=>'Y'
)
,'user_copy'=>array(
'label'=>'Re-enter Username'
,'max'=>60
,'required'=>'Y'
)
);
// form values
$arrValues = array();
// form errors
$arrErrors = array();
$boolError = false;
// determine form values for users
for($i=0;$i<$intUsers;$i++) {
$arrValues[$i] = array();
$arrErrors[$i] = array();
foreach(array_keys($arrConfigs) as $strField) {
$arrValues[$i][$strField] = isset($_POST[$strBaseKey],$_POST[$strBaseKey][$i],$_POST[$strBaseKey][$i][$strField])?$_POST[$strBaseKey][$i][$strField]:'';
if(isset($_POST[$strBaseKey]) && strcmp($arrConfigs[$strField]['required'],'Y') == 0 && strlen($arrValues[$i][$strField]) == 0) {
$boolError = true;
$arrErrors[$i][$strField] = "Field {$arrConfigs[$strField]['label']} is required";
}
}
}
// save data when no errors exist
if($boolError === false && isset($_POST[$strBaseKey])) {
$arrSave = array();
foreach($arrValues as $arrUser) {
$arrCols = array_diff(array_keys($arrUser),array('user_copy','pass_copy','email_copy'));
// @TODO: clean data before imploding
$arrSave[] = "('".implode("','",array_diff_key($arrUser,array('user_copy'=>'','pass_copy'=>'','email_copy'=>'')))."')";
}
$strSQL = sprintf(
'INSERT INTO %s (%s) VALUES %s'
,'users'
,implode(',',$arrCols)
,implode(',',$arrSave)
);
echo "<p>$strSQL</p>";
}
/*
* --------------------------------- Template -----------------------------------
*/
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Form</title>
<style type="text/css">
form fieldset input.invalid {
border: 1px solid red;
}
</style>
</head>
<body>
<?php
// print errors
foreach($arrErrors as $intUser=>$arrError) {
if(empty($arrError)) continue;
echo '<p>Please fill out all required fields for user ',($intUser+1),' before proceeding.</p>';
}
// build form
printf('<form action="%s" method="%s">',$strAction,'POST');
foreach($arrValues as $intUser=>$arrUser) {
printf('<fieldset id="user-%s-registration">',($intUser+1));
printf('<legend>Register %s%s User</legend>',($intUser+1),$arrPostFix[$intUser]);
foreach($arrConfigs as $strField=>$arrConfig) {
if(in_array($strField,array('email','email_copy'))) {
printf('<div class="block %s">',strcmp($strField,'email') == 0?'l':'r');
}
printf(
'<label for="user-%s-%u">%s%s: <input type="%s" name="%s[%u][%s]" maxlength="%u" value="%s" id="user-%1$s-%2$u"%s></label>'
,str_replace('_','-',$strField)
,($intUser+1)
,htmlentities($arrConfig['label'])
,strcmp($arrConfig['required'],'Y') == 0?'<span class="required">*</span>':''
,in_array($strField,array('pass','pass_copy'))?'password':'text'
,$strBaseKey
,$intUser
,$strField
,$arrConfig['max']
,$arrUser[$strField]
,isset($arrErrors[$intUser][$strField])?'class="invalid"':''
//,isset($arrErrors[$intUser][$strField])?"<p>{$arrErrors[$intUser][$strField]}</p>":''
);
if(in_array($strField,array('user','user_copy'))) {
echo '</div>';
}
}
echo '</fieldset>';
}
echo '<input class="submit" type="submit" value="Submit" name="submit" />';
echo '</form>';
?>
</body>
</html>