需要一个使用foreach的逻辑

时间:2013-02-12 06:28:46

标签: php logic weblogic

我有一个下表显示

Task No.    Person            Company           Project Task               Date Tracked Total Time  Comments
14.1    vaish, arpit          ABC-TecHolding    Relaunch Websitnalysis     2013-02-12   19:21:03    
14.2    vaish, arpit          Sugarion          New Opensource Server      2013-02-12   19:21:03    
14.6    vaish, arpit          Sugarion          New Opensource Server      2013-02-12   19:21:03    
5.26    Projectmanager, Paul  ABC-Tec Holding   Relaunch Website           2010-03-13   03:40:16    
5.27    Projectmanager, Paul  ABC-Tec Holding   Relaunch Website           2010-03-12   03:40:16    
5.27    Projectmanager, Paul  ABC-Tec Holding   Relaunch Website           2010-03-13   03:40:16    
8.2     Worker, Willi         Customers ACME    Hosting Data transfer      2010-03-13   00:21:18    
14.2    Worker, Willi         Sugarion          New Opensource Server      2013-02-06   00:21:18    fyhbvghjukjjkhhlhh uuiuijujj jookljh

这是我的foreach循环

foreach($result123 as $key=>$task) {
            $time=$this->addtime($task['name'],$range['start'],$range['end']); //to calculate total time per user in time range

            $diff       = $task['estimated']+$task['tracked'];
            $diffSign   = $diff > 0 ? '-' : '+';

            $table->addRow(array(
                'number'    => $task['tasknumber'].' '.$task['id_project'] ,
                'person'=> $task['name'],
                'customer'      => $task['company'],
                'project'       => $task['project'],
                'task'      => $task['task'],
                'date'      => $task['date_tracked'],
                'tracked'   => $task['workload_tracked'],
                'Total_Time'        => $time,
                'comment'=>$task['comment']
            ));

        }

列总时间显示每人跟踪的总时间。 我希望每个人只能显示一次总时间,而不是每行显示一次。 提前谢谢。

2 个答案:

答案 0 :(得分:0)

我就是这样做的(Pseudocode):

  • 设置一组“已检查”的人
  • Foreach记录:
  • 是否处于“已检查”数组?
  • 如果是,请继续。
  • 如果不是,则显示总数并将人员添加到“已检查”的人。

在PHP中:(如果我做对了)

  $checked = array();

  foreach($result123 as $key=>&$task) {
        $time=$this->addtime($task['name'],$range['start'],$range['end']); //to calculate total time per user in time range

        $diff       = $task['estimated']+$task['tracked'];
        $diffSign   = $diff > 0 ? '-' : '+';

        if (in_array($task['name'],$checked) $time =0;
        else {
              $checked[] = $task['name'];
        }

        $table->addRow(array(
            'number'    => $task['tasknumber'].' '.$task['id_project'] ,
            'person'=> $task['name'],
            'customer'      => $task['company'],
            'project'       => $task['project'],
            'task'      => $task['task'],
            'date'      => $task['date_tracked'],
            'tracked'   => $task['workload_tracked'],
            'Total_Time'        => $time,
            'comment'=>$task['comment']
        ));

    }

答案 1 :(得分:0)

$name = '';

foreach($result123 as $key=>$task) {


if ($name == $task['name']) {
$time = '';
}
elseif ($name != $task['name']) {
$time=$this->addtime($task['name'],$range['start'],$range['end']); 
$name = $task['name'];
}


$diff= $task['estimated']+$task['tracked'];
$diffSign= $diff > 0 ? '-' : '+';

$table->addRow(array(
'number'    => $task['tasknumber'].' '.$task['id_project'] ,
'person'=> $task['name'],
'customer'      => $task['company'],
'project'       => $task['project'],
'task'      => $task['task'],
'date'      => $task['date_tracked'],
'tracked'   => $task['workload_tracked'],
'Total_Time'        => $time,
'comment'=>$task['comment']
));

}

我认为应该这样做。