I have a function that copies values from an array into a (usually) HTML template.
The function is:
function ParmsToTemplate($parm_array, $template)
{
foreach ($parm_array as $field_name => $value)
{
if (!is_array($value))
{
$field_name = '<!--' . strtoupper($field_name) . '-->';
$template = str_replace($field_name, $value, $template);
}
}
return $template;
}
I typically feed it a row from a database result, and a template like this:
var $analyze_time_row_tmplt =
'<tr>
<td valign="top" class="blk_11_13_0_ctr" bgcolor="<!--DEFAULT_LIGHT-->"><!--EMPLOYEE--></td>
<td valign="top" class="blk_11_13_0_ctr" bgcolor="<!--DEFAULT_LIGHT-->"><!--DATE--></td>
<td valign="top" class="blk_11_13_0_ctr" bgcolor="<!--DEFAULT_LIGHT-->"><!--CLIENT_CODE--></td>
<td valign="top" class="blk_11_13_0" bgcolor="<!--DEFAULT_LIGHT-->"><!--PROJECT_ID--> <!--PROJECT_TITLE--></td>
<td valign="top" class="blk_11_13_0" bgcolor="<!--DEFAULT_LIGHT-->"><a href="projects.php?project_time=true&employee_id=<!--EMPLOYEE_ID-->&editentry=true&time_id=<!--TIME_ID-->&date=<!--DATE_RAW-->"><!--ACTIVITY_NAME--></a></td>
<td valign="top" class="blk_11_13_0_ra" bgcolor="<!--DEFAULT_LIGHT-->"><!--HOURS--></td>
<td valign="top" class="blk_11_13_0_ra" bgcolor="<!--DEFAULT_LIGHT-->"><!--RATE--></td>
<td valign="top" class="blk_11_13_0_ra" bgcolor="<!--DEFAULT_LIGHT-->"><!--HOURS_NET--></td>
<td valign="top" class="blk_11_13_0" bgcolor="<!--DEFAULT_LIGHT-->"><!--NOTES--></td>
<td valign="top" class="blk_11_13_0_ctr" bgcolor="<!--DEFAULT_LIGHT-->"><!--INVOICE_ID--></td>
</tr>';
The database row contains values for employee, date, etc. The function runs strtoupper() on each of these, checks to see if they exist in the template (surrounded by HTML comment tag) and if they do, replaces the placeholder with the value.
It works great and makes for (I think) good separation of content and formatting. BUT...
I'm currently developing a system that generates many more results than I've ever had to deal with before. The system is choking when I have a lot of results and this function seems to be the bottleneck. I know this because I replaced it in my code with a more procedural version - i.e. instead of
$table['tablerows'] .= $page->ParmsToTemplate($row, $html->analyze_time_row_tmplt);
for each row of the result, I do
$table['tablerows'] .= '<tr>
<td valign="top" class="blk_11_13_0_ctr" bgcolor="<!--DEFAULT_LIGHT-->">' . $employee_initials[$row['employee_id']] . '</td>
<td valign="top" class="blk_11_13_0_ctr" bgcolor="<!--DEFAULT_LIGHT-->">' . $row['date'] . '</td>
<td valign="top" class="blk_11_13_0_ctr" bgcolor="<!--DEFAULT_LIGHT-->">' . $project_info[$row['project_id']]['client_code'] . '</td>
<td valign="top" class="blk_11_13_0" bgcolor="<!--DEFAULT_LIGHT-->">' . $row['project_id'] . ' ' . $project_info[$row['project_id']]['project_title'] . '</td>
<td valign="top" class="blk_11_13_0" bgcolor="<!--DEFAULT_LIGHT-->"><a href="projects.php?project_time=true&employee_id=' . $row['employee_id'] . '&editentry=true&time_id=' . $row['time_id'] . '&date=' . $row['date_raw'] . '">' . $row['activity_id'] . ' ' . $activity_info[$row['activity_id']]['activity_name'] . '</a></td>
<td valign="top" class="blk_11_13_0_ra" bgcolor="<!--DEFAULT_LIGHT-->">' . $row['hours'] . '</td>
<td valign="top" class="blk_11_13_0_ra" bgcolor="<!--DEFAULT_LIGHT-->">' . $row['rate'] . '</td>
<td valign="top" class="blk_11_13_0_ra" bgcolor="<!--DEFAULT_LIGHT-->">' . number_format(($row['hours'] * $row['rate']), 2, '.', ',') . '</td>
<td valign="top" class="blk_11_13_0" bgcolor="<!--DEFAULT_LIGHT-->">' . $row['notes'] . '</td>
<td valign="top" class="blk_11_13_0_ctr" bgcolor="<!--DEFAULT_LIGHT-->">' . $row['invoice_id'] . '</td>
</tr>';
and it can handle far more results.
What should I do? I suppose for this one page I could just code it procedurally, but I feel there's a better way.
The questionable function exists inside a class that is the base class for my Page class (initiated at the beginning of the script.
Should I use references (something I have a very poor understanding of) when passing the row to the function? Are there parts of the function that could be improved on (str_replace() for example - though I thought that was a more efficient option than some of the other functions)?
Thanks!