我有一些数据库查询。 最后我需要一个像这样的json对象
[{ activity_type:'Develoement', spent_time:[ {user:'User1', time:'21'}, // (!) amount of time for this user and this activity {user:'User2', time:'2'}, ], total_spent_time: 23 },{ activity_type:'Design', spent_time:[ {user:'User', time:'14'}, ], total_spent_time: 14 }]
这是我的sql-query
的while循环while($row = $result->fetch_assoc()){ $data[$row['activity_type']] = array('spent_time' => array('user' => (object)$row['user'])); $data[$row['activity_type']]['spent_time']['user']->time += $row['spent_time']; }
但是在结果数组中我有
[Design] => Array ( [spent_time] => Array ( [user] => stdClass Object ( [scalar] => John [time] => 1.5 // (!) only the last activity time, NOT the sum of spent time for this activity ) ) ) [Developement] => Array ( [spent_time] => Array ( [user] => stdClass Object ( [scalar] => Nick [time] => 0.3 // (!) only the last activity time, NOT the sum of spent time for this activity ) ) )
如何将当前用户和当前活动的循环时间值相加?
UPD 这是查询
$sql = "SELECT
distinct(projects.name) as project,
time_entries.hours as spent_time,
enumerations.name as activity_type,
versions.name as version,
users.lastname as user
FROM `projects`
INNER JOIN time_entries ON ( time_entries.project_id = projects.id )
INNER JOIN enumerations ON ( time_entries.activity_id = enumerations.id )
INNER JOIN versions ON ( projects.id = versions.project_id )
inner join users on (time_entries.user_id = users.id)
where (projects.identifier = 'test')";
答案 0 :(得分:0)
也许像......
class ActivityList{
private $activities;
public function __construct(){
$this->activities=array();
}
public function addActivity($row){
//read the row, search for the activity
//if does not exist, create a new activity
//if does, get it and use addUser to add more time
...
}
}
class Activity{
private $activity_type;
private $spent_time;
private $total_spent_time;
public function __construct(){
$this->spent_time=array();
$this->activity_type="";
$this->total_spent_time=0;
}
public function addUser($user, $timeSpent){
//search for the user in spent_time
//if not exist, create a new user
//if exist, add the time
//once added, take the time and add to total_spent_time
...
}
}
class UserObject{
private $user;
private $time;
public function __construct(){
$this->user="";
$this->time=0;
}
}