This is a follow-up question to my previous post about array posting. I was advised to combine my arrays in order to dynamically generate table results for an email. So far, it looks like this:
$emp_names = is_array($_POST['emp_name']) ? $_POST['emp_name'] : array($_POST['emp_name']);
$emp_today_results = is_array($_POST['emp_today_result']) ? $_POST['emp_today_result'] : array($_POST['emp_today_result']);
$emp_today_goals = is_array($_POST['emp_today_goal']) ? $_POST['emp_today_goal'] : array($_POST['emp_today_goal']);
$emp_month_results = is_array($_POST['emp_month_result']) ? $_POST['emp_month_result'] : array($_POST['emp_month_result']);
$emp_month_goals = is_array($_POST['emp_month_goal']) ? $_POST['emp_month_goal'] : array($_POST['emp_month_goal']);
$emp_month_trends = is_array($_POST['emp_month_trend']) ? $_POST['emp_month_trend'] : array($_POST['emp_month_trend']);
$emp_results = array_combine($emp_names, $emp_today_results, $emp_today_goals, $emp_month_results, $emp_month_goals, $emp_month_trends);
That collects and combines all the arrays. Then, I don't now how to use them. This doesn't work (I assume because there is no "as" in the brackets. Any idea how to make it work?
foreach ($emp_results) {
$htmlBody .= "
<tr>
<td>{$emp_name}</td>
<td>{$emp_today_result}</td>
<td>{$emp_today_goal}</td>
<td>{$emp_month_result}</td>
<td>{$emp_month_goal}</td>
<td>{$emp_month_trend}</td>
</tr>";
}
$values = explode('|', 'emp_name|emp_today_result|emp_today_goal|emp_month_result|emp_month_goal|emp_month_trend');
foreach ($values as $value) {
$$value = (array)$_POST[$value];
}
for ($i = 0; $i < count($emp_name); $i++) {
$emp_results = array();
foreach ($values as $value) {
$emp_results[] = ${$value}[$i];
}
$htmlBody .= '<tr><td>' . implode('</td><td>', $emp_results) . '</td></tr>';
}
Do like this.. You don't need array_combine
here
$emp_names = is_array($_POST['emp_name']) ? $_POST['emp_name'] : array($_POST['emp_name']);
$emp_today_results = is_array($_POST['emp_today_result']) ? $_POST['emp_today_result'] : array($_POST['emp_today_result']);
$emp_today_goals = is_array($_POST['emp_today_goal']) ? $_POST['emp_today_goal'] : array($_POST['emp_today_goal']);
$emp_month_results = is_array($_POST['emp_month_result']) ? $_POST['emp_month_result'] : array($_POST['emp_month_result']);
$emp_month_goals = is_array($_POST['emp_month_goal']) ? $_POST['emp_month_goal'] : array($_POST['emp_month_goal']);
$emp_month_trends = is_array($_POST['emp_month_trend']) ? $_POST['emp_month_trend'] : array($_POST['emp_month_trend']);
for($i=0;$i<count($emp_names);$i++)
{
$htmlBody .= "
<tr>
<td>$emp_name[$i]</td>
<td>$emp_today_result[$i]</td>
<td>$emp_today_goal[$i]</td>
<td>$emp_month_result[$i]</td>
<td>$emp_month_goal[$i]</td>
<td>$emp_month_trend[$i]</td>
</tr>";
}
echo $htmlBody;