在 codeigniter 中添加选定范围内的列值

add column values from a selected range in code igniter

时间记录 table 我有

date
time in 
time out 
break in
break out
total_time 

我的字段 table。我有这个 sql 查询

public function showattendance(){

    $start      = $this->input->post('DATE_FROM');
    $end        = $this->input->post('DATE_TO');
    $employee   = $this->input->post('ID_NUM');

    $sql = "SELECT * FROM `attendance` WHERE (DATE BETWEEN '".$start."' AND '".$end."') AND (ID_NUM = '".$employee."')";

    $query = $this->db->query($sql);

    return $query->result();
}

现在我想做的就是当我已经选择了我选择的日期之间的行时,我想添加从 total_time 列中选择的数据。

例如我得到了一个

的数组
array = (
ID[0] => '1' , TOTAL_TIME[0]=>'09:30'
ID[1] => '2' , TOTAL_TIME[1]=>'07:30'
ID[2] => '3' , TOTAL_TIME[2]=>'08:30'
ID[3] => '4' , TOTAL_TIME[3]=>'09:00'
ID[4] => '5' , TOTAL_TIME[4]=>'05:45'

);

我只想添加

TOTAL_TIME[0]+TOTAL_TIME[1]+TOTAL_TIME[2]+TOTAL_TIME[3]+TOTAL_TIME[4] = $value

据我从你的问题中了解到,你需要添加从 start_date 到 end_date 的句点才能做到这一点 运行 此代码:

$res = $this->showattendance();// this is from your code
$this->db->update("time-recods");
$this->db->set("total_time",date_diff($res->time_in,$res->time_out));
$this->db->where('ID_NUM',$employee); 

希望对你有所帮助:)

当您将结果转换为数组时,这样做会变得非常简单。这可能会帮助您解决问题。

Here one column is later updated with new values in php. This may be helpful.

----------------更新答案------------

那么你的数组应该是这样的

<?php
$x =  array(
    array( ID => '1' , TOTAL_TIME =>'09:30'),
    array( ID => '2' , TOTAL_TIME =>'07:30'),
    array( ID => '3' , TOTAL_TIME =>'08:30'),
    array( ID => '4' , TOTAL_TIME =>'09:00'),
    array( ID => '5' , TOTAL_TIME =>'05:45'));

$total;
foreach($x as $y){
    $total+=$y["TOTAL_TIME"];
}
echo "Total Time = ".$total;

?>

这样就可以得到你的总时间。