Skip to main content
6 of 6
Added SQL Server tag
Phrancis
  • 20.5k
  • 6
  • 70
  • 155

Get and display employee time clock data

I have been making a web page in PHP and JavaScript to take employees clock times and do some things with them so I can display them. Everything is currently working quite well, but the code looks rough.

<html>        <meta http-equiv="refresh" content="1; URL=http://localhost/T_A/People.php"></html>
Op = $OpNo; } Public function GetPersonData() { $conn = odbc_connect('easydo', '', ''); if (!$conn) { exit("Connection Failed: " . $conn); } $sql = "SELECT ATDSchedulingRule.Name, ATDSchedulingRule.Remark, ATDShiftDetail.WorkHour, RIGHT(ATDShiftDetail.StartTime, 6) AS StartTime, RIGHT(ATDShiftDetail.EndTime, 6) AS EndTime, RIGHT(CHINA_VISION_PubPersonnel.ID, 4) AS ClockNo, CHINA_VISION_PubCards.CardCode FROM ATDShiftDetail INNER JOIN ATDSchedulingRule ON ATDShiftDetail.ATDShift_Ref = ATDSchedulingRule.Shift_Ref2 INNER JOIN CHINA_VISION_PubCards INNER JOIN CHINA_VISION_PubPersonnel ON CHINA_VISION_PubCards.PubPersonnel_Ref = CHINA_VISION_PubPersonnel.Reference ON ATDSchedulingRule.Name = RIGHT(CHINA_VISION_PubPersonnel.ID, 4) WHERE (ATDShiftDetail.WorkHour > 0) AND (CHINA_VISION_PubPersonnel.Telephone > '1') AND ATDSchedulingRule.Name = '$this->Op' "; $rs = odbc_exec($conn, $sql); if (!$rs) { exit("Error in SQL"); } $Arr = array(); while (odbc_fetch_row($rs)) { $WHR = round((odbc_result($rs, "WorkHour")), 2); $Arr = array('Op' => (odbc_result($rs, "Name")), 'Name' => (odbc_result($rs, "Remark")), 'WorkHours' => $WHR, 'Start' => (odbc_result($rs, "StartTime")), 'End' => (odbc_result($rs, "EndTime")), 'CardNo' => (odbc_result($rs, "CardCode")),); $CardCode = (odbc_result($rs, "CardCode")); } return $Arr; } Public function GetTimes() { $Data = $this->GetPersonData(); $CardNo = $Data['CardNo']; $conn = odbc_connect('easydo', '', ''); if (!$conn) { exit("Connection Failed: " . $conn); } $sql = "SELECT TOP (4) DorCtrls_Ref, EventsID, EventTM, CardCode FROM CHINA_VISION_DorEvents WHERE (CardCode = '$CardNo') AND (DorCtrls_Ref = '16') AND (EventTM >= DATEADD(hour, 0, DATEADD(day, 0, CAST(CAST(GETDATE() AS date) AS datetime)))) ORDER BY EventTM Asc "; $rs = odbc_exec($conn, $sql); if (!$rs) { exit("Error in SQL"); } $data = array(); while (odbc_fetch_row($rs)) { $data[] = $this->EventTM = odbc_result($rs, "EventTM"); } //var_dump($this->Swipetimes); $Starttime = strtotime($Data['Start']) + 2 * 60 * 60; @$Clocktime = strtotime($data[0]) + 2 * 60 * 60; $late = $Starttime - $Clocktime; $lated = $late / 60; $Endtime = strtotime($Data['End']) + 2 * 60 * 60; @ $Out = strtotime($data[3]) + 2 * 60 * 60; $home = $Endtime - $Out; $homed = $home / 60; echo '

'; $Arr = @array('Op' => $Data['Op'], 'Name' => $Data['Name'], 'WorkHours' => $Data['WorkHours'], 'Start' => $Data['Start'], 'End' => $Data['End'], 'Clock' => substr($data[0], 11, 8), 'OFF' => substr($data[1], 11, 8), 'ON' => substr($data[2], 11, 8), 'OUT' => substr($data[3], 11, 8), 'Late' => $lated, 'Home' => $homed); return $Arr; } } $People = (object) array( 'People' => array( array( 'Person1' => $person1->GetTimes(), ), array( 'Person2' => $person2->GetTimes(), ), array( 'Person3' => $person3->GetTimes(), ), array( 'Person4' => $person4->GetTimes(), ), array( 'Person5' => $person5->GetTimes(), ), array( 'Person6' => $person6->GetTimes(), ), array( 'Person7' => $person7->GetTimes(), ), array( 'Person8' => $person8->GetTimes(), ), array( 'Person9' => $person9->GetTimes(), ), array( 'Person10' => $person10->GetTimes(), ), array( 'Person11' => $person11->GetTimes(), ), array( 'Person12' => $person12->GetTimes(), ), array( 'Person13' => $person13->GetTimes(), ), array( 'Person14' => $person14->GetTimes(), ), array( 'Person15' => $person15->GetTimes(), ), array( 'Person16' => $person16->GetTimes(), ), array( 'Person17' => $person17->GetTimes(), ), array( 'Person18' => $person18->GetTimes(), ), array( 'Person19' => $person19->GetTimes(), ), array( 'Person20' => $person20->GetTimes(), ), array( 'Person21' => $person21->GetTimes(), ), array( 'Person22' => $person22->GetTimes(), ), array( 'Person23' => $person23->GetTimes(), ), array( 'Person24' => $person24->GetTimes(), ), array( 'Person25' => $person25->GetTimes(), ), array( 'Person26' => $person26->GetTimes(), ), array( 'Person27' => $person27->GetTimes(), ), array( 'Person28' => $person28->GetTimes(), ), array( 'Person29' => $person29->GetTimes(), ), array( 'Person30' => $person30->GetTimes(), ), array( 'Person31' => $person31->GetTimes(), ), array( 'Person32' => $person32->GetTimes(), ), array( 'Person33' => $person33->GetTimes(), ), array( 'Person34' => $person34->GetTimes(), ), array( 'Person35' => $person35->GetTimes(), ), array( 'Person36' => $person36->GetTimes(), ), array( 'Person37' => $person37->GetTimes(), ), array( 'Person38' => $person38->GetTimes(), ), array( 'Person38' => $person39->GetTimes(), ), array( 'Person38' => $person40->GetTimes(), ), array( 'Person38' => $person41->GetTimes(), ), ) ); $time_end = microtime(true); file_put_contents('People.json', json_encode($People)); $execution_time = round((($time_end - $time_start) / 60), 3); echo 'Total Execution Time: ' . $execution_time . ' Mins '; This code takes people that I have imitated in the included `People` select file, runs some queries and joins the data, puts it all into an array and then in to a JSON file. However, there must be a better way of doing what I have done here. Here is how I am picking the people in the PeopleSelect.php file: "); break; case 6: print("Today is Saturday"); break; }

Josh Kirkpatrick
  • 1.1k
  • 1
  • 10
  • 23