This topic is locked
[SOLVED]

Show Calculated Field in Master Table

5/4/2021 11:22:24 PM
PHPRunner General questions
J
JoeB7774 author

I am creating a time sheet database and running into a snag. I have a master table (tblTimeSheet) and an assoicated detail table (tblTimeSheet). What I've ended up doing is creating a custom view for each user (myTimeSheet/myTimeSheetDetail) and added an additional where clause so that each user only sees their entries. Where I'm running into a snag is that I've been asked to group the time sheets into two week blocks (pay periods). I did this by creating a table (tblPayPeriod ... and Associated PayPeriod view) with the pay periods in it and it's working out quite well. I just end up with PayPeriod->myTimeSheet->myTimeSheetDetail. My snag is how I summarize the data within PayPeriod.

To begin, I've created a text field called TotalHours on the PayPeriod list view. I then went into the 'After Record Added' of the myTimeSheetDetail and added the following code (based on looking through similar threads on this forum).

global $dal;

$TimeSheetDetailID = $values['TimeSheetDetailID'];
$TimeSheetID = $values['TimeSheetID'];
$rs=CustomQuery("SELECT PPID FROM tblTimeSheet WHERE TimeSheetID=".$TimeSheetID);
$data=db_fetch_array($rs);

$PayPeriodID = $data['PPID'];
$userData = Security::getUserName();

$rs2 = CustomQuery("SELECT tblTimeSheet.Employee, Sum(tblTimeSheetDetail.Hours) AS SumOfHours, tblTimeSheet.PPID
FROM tblTimeSheet LEFT JOIN tblTimeSheetDetail ON tblTimeSheet.TimeSheetID = tblTimeSheetDetail.TimeSheetID
GROUP BY tblTimeSheet.Employee, tblTimeSheet.PPID
HAVING tblTimeSheet.Employee='".$userData."' AND tblTimeSheet.PPID=".$PayPeriodID);

$data2 = db_fetch_array($rs2);
echo $data2['SumOfHours'];

$dal->PayPeriod->Value["TotalHours"] = $data2['SumOfHours'];
$dal->PayPeriod->Update();

Unfortuneately I get an error on the 'Update()' portion of the code (call to undefined method). I'm not sure if it's because Im using a custom view (and should switch back to the tables). I also would like the totals for each pay period to be there when the PayPeriod list page opens. I was just wondering if someone could point me in the right direction to do this.

Thanks in advance.

admin 5/5/2021

It is hard to tell what exactly is wrong with this code without seeing the whole project but I certainly recommend using Database API instead of deprecated DAL functions:
DB::Update()

J
JoeB7774 author 5/12/2021

Thanks I'll check it out.