我想从数据库中获取每个用户的登录和注销条目。
现在可以通过传递用户的emp_id
来获取登录或注销。因此,无论我在哪个用户中传递条件,都会获取该用户的登录和注销数组。
现在我有多个用户,因此想要获取每个用户的登录注销条目。所以我用于每个循环并循环用户emp_id
。并将其传递给我的模型方法,以便我可以传递所有emp_id
并获取每个人的登录和注销详细信息。
现在这就是我在控制器中做的事情:
$users = $this->data['users'] = $this->user_m->get();
foreach ($users as $user)
{
$emp_id = $user->emp_id;
$all_login = $this->data['attendances_logins'] = $this->attendance_m->all_login($emp_id);
$all_logout = $this->data['attendances_logouts'] = $this->attendance_m->all_logout($emp_id);
}
$this->data['subview'] = 'admin/dashboard/index';
$this->load->view('admin/_layout_main',$this->data);
因此,如果我var_dump($users);
,我将获得所有用户的所有数组。
如果我var_dump($all_login/all_logout);
,我将获得每个用户的登录入口和注销条目。
现在在我看来,如果我这样做
var_dump($attendances_logins);
我只是获取最后一位用户的数据。为什么它不像我进入控制器那样打印每个用户的登录和注销条目?
这是我的代码,用于获取我已经通过$emp_id
的登录和注销条目,因此它返回每个用户的登录和注销:
public function all_login($emp_id)
{
$startDate = new DateTime("first day of last month");
$endDate = new DateTime("last day of last month");
$this->db->select('*');
$this->db->from('daily_data2');
$this->db->where('users.emp_id',$emp_id);
$this->db->where('entry >','100');
$this->db->where("date(date_data) BETWEEN '" . $startDate->format( 'Y-m-d' ) . "' AND '" . $endDate->format( 'Y-m-d') . "' ");
$this->db->order_by("date_data","ASC");
$this->db->join('users', 'users.emp_id = daily_data2.emp_id','inner');
$query = $this->db->get();
$res = $query->result();
return $res;
}
public function all_logout($emp_id)
{
$startDate = new DateTime("first day of last month");
$endDate = new DateTime("last day of last month");
$this->db->select('*');
$this->db->from('daily_data2');
$this->db->where('users.emp_id',$emp_id);
$this->db->where('entry <','100');
$this->db->where("date(date_data) BETWEEN '" . $startDate->format( 'Y-m-d' ) . "' AND '" . $endDate->format( 'Y-m-d') . "' ");
$this->db->order_by("date_data","ASC");
$this->db->join('users', 'users.emp_id = daily_data2.emp_id','inner');
$query = $this->db->get();
$res = $query->result();
return $res;
}
计算代码::
<?php
$loginarry = $attendances_logins;
$logoutarry =$attendances_logouts;
$timeduration=0;
$login_i=0;
$logout_i=0;
echo '<table border="1px" class="table table-striped table-bordered table-hover" id="dataTables-example">';
echo '<tr><th>Login</th><th>Logout</th><th>Duration</th></tr>';
//echo ("Array Length = " . count($loginarry)." & ".count($logoutarry)."\n<br/>");
while(true)
{
if( $login_i >= count($loginarry) && $logout_i >= count($logoutarry))
break;
if( $logout_i >= count($logoutarry))
{
//echo ("Login[".$login_i."] : ".$loginarry[$login_i]->date_data." and Logout : -----------------\n<br/>");
echo(&#34;&#34;。$ loginarry [$ login_i] - &gt; date_data。&#34; Not AvailableNot Available&#34;);
// echo ("<tr> <td>".$loginarry->date_data." </td><td> Data Not Available</td></tr>");
$login_i++;
continue;
}
if(($login_i+1) == count($loginarry) && ($logout_i+1) < count($logoutarry))
{
echo ("<tr><td>Data Not Available </td><td>".$logoutarry[$logout_i]->date_data."\n</td><td>Data Not Available</td></tr>");
$logout_i++;
continue;
}
if( $login_i >= count($loginarry))
{
echo ("<tr><td> Data Not Available </td><td>".$logoutarry[$logout_i]->date_data."\n<td>Data Not Available</td></td></tr>");
$logout_i++;
continue;
}
//echo( "******* ".(new DateTime($loginarry[$login_i+1]->date_data))->format('U') . " **** ".(new DateTime($logoutarry[$logout_i]->date_data))->format('U'). "\n<br/>");
//check if next login time is smaller then current logout time - if so skip current login time
if($login_i < (count($loginarry)-1) && (new DateTime($logoutarry[$logout_i]->date_data))->format('U') > (new DateTime($loginarry[$login_i+1]->date_data))->format('U'))
//if( date_diff(date_create($logoutarry[$logout_i]->date_data), date_create($loginarry[$login_i+1]->date_data)) > 0 )
{
echo ("<tr><td>".$loginarry[$login_i]->date_data."</td><td>Data Not Available</td><td>Data Not Available</td></tr>");
$login_i++;
continue;
}
//
if( (new DateTime($loginarry[$login_i]->date_data))->format('U') > (new DateTime($logoutarry[$logout_i]->date_data))->format('U') )
//if( date_diff(date_create($loginarry[$login_i]->date_data), date_create($logoutarry[$logout_i]->date_data)) > 0 )
{
echo ("<tr><td>Data Not Available </td><td>".$logoutarry[$logout_i]->date_data."\n</td><td>Data Not Available</td></tr>");
$logout_i++;
continue;
}
//if more logout entries then skip to last logout entry
if( $login_i < (count($loginarry)-1) && $logout_i < (count($logoutarry)-1) && (new DateTime($logoutarry[$logout_i+1]->date_data))->format('U') < (new DateTime($loginarry[$login_i+1]->date_data))->format('U'))
{
echo(" <tr><td> Data Not Available</td> <td>".$logoutarry[$logout_i]->date_data."\n</td> <td>Data Not Available</td> </tr>");
$logout_i++;
continue;
}
echo ("<tr><td> ".$loginarry[$login_i]->date_data."</td> <td>".$logoutarry[$logout_i]->date_data."</td>");
$singleduration = ((new DateTime($logoutarry[$logout_i]->date_data))->format('U') - (new DateTime($loginarry[$login_i]->date_data))->format('U'));
if($singleduration > 43200)//12 hours X 3600 seconds)
{
//$timeduration += $singleduration;
echo ("<td class='danger'>Huge duration - not considered - please contact HR (".$singleduration.")</td></tr>");
}
else
{
$timeduration += $singleduration;
echo ("<td>".gmdate("H:i:s",$singleduration)."</td></tr>");
}
$login_i++;
$logout_i++;
}
echo ("</table>");
//$finaltime=gmdate("H:i:s",$timeduration);
//$timedurationstr = ConvertSecondsToStr($timeduration);
$hours = 0;
if($timeduration > 3600)
{
$hours = floor($timeduration/3600);
$timeduration -= (3600*$hours);
}
$min = 0;
if($timeduration > 60)
{
$min = floor($timeduration/60);
$timeduration -= (60*$min);
}
$seconds = $timeduration;
//echo("<div class= 'row'> <div class ='col-sm-4' style= 'float:right'><h3 class='alert alert-success'><center>Total Hours Worked ". $finaltime." Hours</center></h3></div></div>");
echo("<div class= 'row'> <div class ='col-sm-4' style= 'float:right'><h3 class='alert alert-success'><center>Total time worked: ". $hours." Hour(s) ".$min." Minute(s) ".$seconds." Second(s)</center></h3></div></div>");
//echo("<div class= 'row'> <div class ='col-sm-4' style= 'float:right'><h3 class='alert alert-success'><center>Total time worked: ". $timedurationstr." </center></h3></div></div>");
?>
模特:
public function all_login($emp_id)
{
$startDate = new DateTime("first day of last month");
$endDate = new DateTime("last day of last month");
$this->db->select('*');
$this->db->from('daily_data2');
$this->db->where('users.emp_id',$emp_id);
$this->db->where('entry >','100');
$this->db->where("date(date_data) BETWEEN '" . $startDate->format( 'Y-m-d' ) . "' AND '" . $endDate->format( 'Y-m-d') . "' ");
$this->db->order_by("date_data","ASC");
$this->db->join('users', 'users.emp_id = daily_data2.emp_id','inner');
$query = $this->db->get();
$res = $query->result();
return $res;
}
public function all_logout($emp_id)
{
$startDate = new DateTime("first day of last month");
$endDate = new DateTime("last day of last month");
$this->db->select('*');
$this->db->from('daily_data2');
$this->db->where('users.emp_id',$emp_id);
$this->db->where('entry <','100');
$this->db->where("date(date_data) BETWEEN '" . $startDate->format( 'Y-m-d' ) . "' AND '" . $endDate->format( 'Y-m-d') . "' ");
$this->db->order_by("date_data","ASC");
$this->db->join('users', 'users.emp_id = daily_data2.emp_id','inner');
$query = $this->db->get();
$res = $query->result();
return $res;
}
答案 0 :(得分:2)
尝试这样的事情
foreach ($users as $user)
{
$emp_id = $user['emp_id'];
$attendances_logins = $this->attendance_m->all_login($emp_id);
$attendances_logouts = $this->attendance_m->all_logout($emp_id);
$login_logout[] = array(
'userId' => $emp_id,
'login' => $attendances_logins, # Count number only
'logout' => $attendances_logouts # Count number only
);
}
$data['login'] = $login_logout
// load view
答案 1 :(得分:1)
我相信你的PHP代码有错误:
$all_login = $this->data['attendances_logins'] = $this->attendance_m->all_login($emp_id);
$all_logout = $this->data['attendances_logouts'] = $this->attendance_m->all_logout($emp_id);
应该像这样设置:
$this->data['attendances_logins'] = array();
$this->data['attendances_logouts'] = array();
foreach ($users as $user)
{
$this->data['attendances_logins']["$emp_id"] = $this->attendance_m->all_login($emp_id);
$this->data['attendances_logouts']["$emp_id"] = $this->attendance_m->all_logout($emp_id);
}