Never mind folks. I figured out what I needed to do.
For those interested it is in the tablename_dv() hook in the tablename.php file where it is done after all. I was confused as this hook only provides you the page's generated HTML and no actual data per se. Penny dropped when I realised there was nothing stopping me running a query in that hook function to get the data, especially as the primary key is ONE of the things provided to the hook.
The code I added now does the calc when viewing a timesheet you have clicked on in the Table View. If in the Table View you click to add a new record, then the code adds a caption for those fields in the detail view indicating a value will appear for those fields after the timesheet is added / updated.
As I said, for those interested, the code is below:
Code: Select all
function tbltimesheets_dv($selectedID, $memberInfo, &$html, &$args)
{
if ($selectedID) // if viewing an existing record then do this lot
{
$id=makeSafe($selectedID);
// get the sum of all the hours for this timesheet - will only return 1 row of 1 column
$res = sql(
"SELECT (tbltimesheets.MondayHours + tbltimesheets.TuesdayHours + ".
"tbltimesheets.WednesdayHours + tbltimesheets.ThursdayHours + ".
"tbltimesheets.FridayHours + tbltimesheets.SaturdayHours + ".
"tbltimesheets.SundayHours) AS ToThours ".
"FROM tbltimesheets ".
"WHERE tbltimesheets.TimesheetID = '$id'"
, $eo);
if (mysql_num_rows($res)) // SQL returned one or more rows?
{
$row = mysql_fetch_row($res);
$th = sprintf("%.2f", $row[0]); // Get total hours
$td = sprintf("%.2f", ($th / 8)); // Get total days
$frh = 'id="TotHours"></p>'; // Set up target "hours" HTML to change
$toh = 'id="TotHours">'.$th.'</p>'; // Set up the "hours" HTML the target will be changed too
$frd = 'id="TotDays"></p>'; // Set up target "days" HTML to change
$tod = 'id="TotDays">'.$td.'</p>'; // Set up the "days" HTML the target will be changed too
$html = str_replace($frh, $toh, $html);
$html = str_replace($frd, $tod, $html);
}
}
else // otherwise, if viewing a "new" empty record do this
{
$frh = 'id="TotHours"></p>';
$toh = 'id="TotHours">Automatically calculated after a timesheet is submitted or updated</p>';
$frd = 'id="TotDays"></p>';
$tod = 'id="TotDays">Automatically calculated after a timesheet is submitted or updated</p>';
$html = str_replace($frh, $toh, $html);
$html = str_replace($frd, $tod, $html);
}
}
