PHP:根据键在foreach中分隔值

时间:2016-10-14 06:53:45

标签: php laravel sorting foreach

我正在使用PHP Laravel。

我需要获得每位员工的总时间/小时数。每个员工都有一个指定的timeKeeping_empID。 Time_Logs表有Employees的时间日志,timeKeeping_empID作为外键。

我尝试运行我的代码: 控制器:

ViewContainerRef.createComponent

在我看来: $ totalHours = 0;

function getTotalHours(Request $request)
        {

            $team = Employees::where('managerID', $request->session()->get('employeeID'))->get();
                //I am assigning an array to be grouped according to my employeeID session.
                $teamIDs = array();
                //I am getting my own session so I can see my data as well together with my team
                array_push($teamIDs, $request->session()->get('employeeID'));
                foreach($team as $member) {
                    array_push($teamIDs, $member->employeeID);
                }
                //I am getting the column employeeID which is the same with the teamID
                $empData = Employees::whereIn('employeeID', $teamIDs)->get();
                $timeKeepingIDs = array();
                foreach($empData as $anEmpData) {
                    array_push($timeKeepingIDs, $anEmpData->timeKeeping_empID);
                }
                $timeLogData = Time_Logs::whereIn('timeKeeping_empID', $timeKeepingIDs)->get();

            return view('welcome', compact('timeLogData','request'));
        }

但结果是所有员工的总工时。 我需要的是每个员工的总小时数。

这是我的数据库表:

        foreach ($timeLogData as $timeLogDatas) {

        $dateToday = new DateTime($timeLogDatas->date);
        $timeFrom = date_create($timeLogDatas->time_in);
        $timeTo = date_create($timeLogDatas->time_out);
        $hoursRendered = date_diff($timeFrom, $timeTo);

        $totalHours=$totalHours += (int) $hoursRendered->format('%h');
        }
        echo "$totalHours";

抱歉,我是PHP的新手。

3 个答案:

答案 0 :(得分:1)

作为MVC模式的原则问题,您不应该将应用程序逻辑放在View中;将所有计算逻辑放在Controller中,并在视图中输出计算值。

话虽如此,您获取所有员工的时间数据之所以是因为您将整个员工ID号数组传递给了timelogdata的Where子句:

$timeLogData = Time_Logs::whereIn('timeKeeping_empID', $ timeKeepingIDs )->get();

通过传递一系列员工编号,您基本上可以获得$ timeKeepingIDs数组中所有员工的合并时间日志。

有很多方法可以解决这个问题,但最简单的方法可能是为每位员工创建一个总计小时数的关联数组。您必须遍历$ empData数组中的每个雇员,执行计算,并保存单独的$ timesheets数组,如:

//I am getting the column employeeID which is the same with the teamID
$empData = Employees::whereIn('employeeID', $teamIDs)->get();

$timesheets = array();
foreach($empData as $anEmpData) {
    $timeLogData = Time_Logs::whereIn('timeKeeping_empID', $anEmpData->timeKeeping_empID)->get();

    $totalHours = 0;

    $timeLogs = array();
    foreach($timeLogData as &$t) {
        $timeData['dateToday'] = new DateTime($t->date);
        $timeData['timeFrom'] = date_create($t->time_in);
        $timeData['timeTo'] = date_create($t->time_out);
        $timeData['hoursRendered'] = date_diff($timeFrom, $timeTo);
        array_push($timeLogs, $timeData);

        $totalHours=$totalHours += (int) $hoursRendered->format('%h');
    }

    $timesheets[$anEmpData->timeKeeping_empID] = [ 'timeLogs' => $timeLogs, 'totalHours' => $totalHours ];
}

return view('welcome', compact('timesheets','request'));

现在,根据您的观点,您可以使用@foreach Blade指令:

<?php
@foreach ($timesheets as $empid => $timesheet)
<p>Timesheet for employee id $empid</p>

    @foreach ($timesheet['timeLogs'] as $timeLog)
        <ul>
        @foreach ($timeLog as $logEntry)

            <li>Date: $logEntry['dateToday']</li>
            <li>Time In: $logEntry['timeFrom']</li>
            <li>Time Out: $logEntry['timeTo']</li>
            <li>Hours rendered: $logEntry['hoursRendered']</li>

        @endforeach
        </ul>
    @endforeach

    Total hours rendered is: $timesheet['totalHours']
@endforeach
?>

我还没有在我的电脑上对此进行测试,但我希望你明白这一点。

希望这有帮助!

答案 1 :(得分:0)

我认为你应该做的是一系列小时而不是将所有这些小时总结在一起:

    foreach ($timeLogData as $timeLogDatas) {

    $dateToday = new DateTime($timeLogDatas->date);
    $timeFrom = date_create($timeLogDatas->time_in);
    $timeTo = date_create($timeLogDatas->time_out);
    $hoursRendered = date_diff($timeFrom, $timeTo);

    $totalHours[$timeLogDatas->timeKeeping_empID] = $totalHours[$timeLogDatas->timeKeeping_empID] + (int) $hoursRendered->format('%h');
    }
    print_r("$totalHours");

然后您将拥有一个包含员工总小时数的数组。 然后你会有一个输出:

Array
(
    [123] => 120
    [124] => 95
    [129] => 195
)

其中密钥是员工ID,以及值,即他的总小时数。

答案 2 :(得分:0)

嘿,我会告诉你Laravel魔法:P

function getTotalHours(Request $request)
{
$timeLogData = Time_Logs::groupBy('timeKeeping_empID')->selectRaw('sum(time_out-time_in) as workingHours, timeKeeping_empID')->lists('workingHours','timeKeeping_empID');
}
在您的视图中

foreach ($timeLogData as $timeLogDatas) {
{{$timeLogDatas->user->name}} - {{$timeLogDatas->workingHours}}
} 

Time_Logs 模型中,您必须使用此功能

public function user()
    {
        return $this->belongsTo('App\Employee','EmployeeID');
    }

EXtra:我猜您必须将此用于 Time_Logs

protected $dates = [
        'log_in',
        'log_out',

    ];