2014-10-28 170 views
0

DB值:For循環不返回所有值

a_id a_user_id a_date  a_intime a_outtime 
1  1  28/10/2014 12.30  1.30 
2  1  29/10/2014 12.30  1.30 
3  1  30/10/2014 12.30  1.30 

for循環只返回一個值。我想要返回所有a_date值。

foreach ($query->result() as $row) {  
    $users= array( 
     'a_user_id' => $row->a_user_id, 
     'a_date'=> $row->a_date, 
     'a_intime'=>$row->a_intime, 
     'a_outtime'=> $row->a_outtime 
    ); 
    } 

    $b=date('g:i a',$bd); 
    $ttime=($a-$b/3600); 
    $total_time=date(' g:i ',$ttime); 

    $user['total_hour']= $total_time; 
    return $user; 

回答

4

你在循環$users必須是一個數組

foreach ($query->result() as $row) 
{ 
    $users[] = array( 
     'a_user_id' => $row->a_user_id, 
     'a_date'=> $row->a_date, 
     'a_intime'=>$row->a_intime, 
     'a_outtime'=> $row->a_outtime 
    ); 
} 

既然是variable它只能容納循環的最後一次出現。