2016-10-02 16 views
1

的考勤記錄我有數據庫表命名爲:時間表它具有下列列和數據:如何創建一個就業報告出來使用PHP

employee_id employee_name year month day timein timeout department 
1   dave   2016 09  15 8  4  finance 
1   dave   2016 09  16 8  4  finance 
1   dave   2016 09  17 8  4  finance 
2   frank   2016 09  15 8  4  purchase 
2   frank   2016 09  16 8  4  purchase 

它記錄員工日常考勤,你可以往上看。我要的是創造這些考勤記錄了使用PHP的就業報告顯示的工作總小時數在一個單獨的HTML表中的每個部門,像這樣的僱員:

財務部門的工資HTML表格

employe_id | employee_name | total_working_hours 
-----------+----------------+--------------------- 
1   | dave   | 24 (8 hrs * 3 days) 

採購部門工資單的HTML表格

employe_id | employee_name | total_working_hours 
-----------+----------------+--------------------- 
1   | frank   | 16 

請注意,我不知道所有emplo的ID的所以代碼應該只列出使用PHP/MYSQL按部門分組的所有人

+0

你爲什麼不使用任何數據庫? –

+0

我的意思是,你使用數據庫系統來存儲和獲取你的數據? –

+0

是的,我使用MYSQL – the7k

回答

0

有幾個視圖可以完成這項工作:一個用於工作時間,一個用於員工,然後您可以加入他們並設置過濾器,如這樣的:

-- Query the working hours 
CREATE VIEW 
vw_working_hours 
AS 
SELECT 
    employee_id, 
    year, 
    month, 
    SUM((timeout+12) - timein) AS total_working_hours 
FROM 
    timesheets 
GROUP BY 
    employee_id, 
    year, 
    month; 

-- Query the employees 
CREATE VIEW 
    vw_employees 
AS 

SELECT 
    DISTINCT 
    employee_id, 
    employee_name, 
    department 
FROM 
    timesheets; 

-- This query is the actual report 
-- just had to filter by department 
-- in your script 
SELECT 
    wh.employee_id, 
    emp.employee_name, 
    wh.total_working_hours 
FROM 
    vw_working_hours AS wh 
JOIN 
    vw_employees AS emp 
ON 
    wh.employee_id = emp.employee_id 
WHERE 
    wh.year = 2016 
    AND 
     wh.month = '09' 
    AND 
     emp.department = 'finance'; 

或者,在一個單一的查詢(無意見):

SELECT 
    wh.employee_id, 
    emp.employee_name, 
    wh.total_working_hours 
FROM 
    (
    SELECT 
     employee_id, 
     year, 
     month, 
     SUM((timeout+12) - timein) AS total_working_hours 
    FROM 
     timesheets 
    GROUP BY 
     employee_id, 
     year, 
     month 

    ) AS wh 
JOIN 
    (
    SELECT 
     DISTINCT 
     employee_id, 
     employee_name, 
     department 
    FROM 
     timesheets 

    ) AS emp 
ON 
    wh.employee_id = emp.employee_id 
WHERE 
    wh.year = 2016 
    AND 
     wh.month = '09' 
    AND 
     emp.department = 'finance'; 

在PHP中,你應該使用一個循環和積累的變量。首先,你應該預過濾器這樣的查詢:

SELECT 
    * 
FROM 
    timesheets 
WHERE 
    department = 'finance' 
    AND 
    year = 2016 
    AND 
    month = '09' 
    ORDER BY 
    employee_id; 

然後,如果結果是在一個名爲$行,東西在PHP這樣的多維數組:

$employee_id = $rows[0]['employee_id']; 
    $employee_name = $rows[0]['employee_name']; 
    $accumulated = 0; 

    foreach($rows as $row) { 
     $total_working_hours = ($row['timeout'] + 12) - $row['timein']; 
     if ($row['employee_id'] == $employee_id) { 
     // Same employee, acumulate 
     $accumulated += $total_working_hours; 
     } else { 
     // Another employee, pass the acumulation to result 
     $rowTmp['employee_id'] = $employee_id; 
     $rowTmp['employee_name'] = $employee_name; 
     $rowTmp['total_working_hours'] = $accumulated; 
     $result[] = $rowTmp; 
     // Updated the accumulation variables 
     // new employee 
     $employee_id = $row['employee_id']; 
     $employee_name = $row['employee_name']; 
     // reset accumulated 
     $accumulated = $total_working_hours; 
     } 
    } 

    // at the end, updates the las result: 
    $rowTmp['employee_id'] = $employee_id; 
    $rowTmp['employee_name'] = $employee_name; 
    $rowTmp['total_working_hours'] = $accumulated; 
    $result[] = $rowTmp; 

    // Should pass result to HTML table 
    print_r($result); 
+0

我會試試這個,然後我會回報。非常感謝。 – the7k

+0

它說:#1050 - 表'vw_working_hours'已經存在 – the7k

+0

是不是有辦法做到這一點在PHP?它對我來說看起來很複雜,因爲我對SQL只有很少的瞭解 – the7k

相關問題