My database structure:
Person
id | name |
1 | John |
2 | Doe |
3 | Marc |
Tasks
task_id | task_name| person_id
1 | Get milk | 1
2 | Play cs | 1
3 | Walk dog | 2
4 | Eat fruit | 3
comments
id | comment | task
1 | Wich one | 1
2 | When? | 2
I tried this:
function get_shapes2() {
$this->db->select('person.name,person.id,')
->select('GROUP_CONCAT(DISTINCT comments.id separator " -/r/- ") as "commentid" ')
->select('GROUP_CONCAT(DISTINCT comments.comment separator " -/r/- ") as "comment" ')
->select('GROUP_CONCAT(DISTINCT tasks.task_name separator " -/r/- ") as "tname"')
->select('GROUP_CONCAT(DISTINCT tasks.task_id separator " -/r/- " ) as "id2"');
$this->db->from('person');
$this->db->join('tasks', 'tasks.person_id = person.id', 'left');
$this->db->join('comments', 'comments.task = tasks.task_id', 'left ');
$this->db->group_by('id');
$query = $this->db->get();
$res = array();
foreach ($query->result() as $row) {
$posts[] = $row->name;
$posts[] = (int) $row->id;
$posts[] = array_map(function($tname, $tid){
return array('tname'=>$name,'tid'=>$tid);
},
explode(" -/r/- ",$row->tname),
explode(" -/r/- ",$row->id2));
array_push($res, $posts);
unset($posts);
}
return $res;
}
I get:
{name: John, id: 1, task =[ {tname: "Get milk", tid: "1"},{tname: "Play cs", tid: "2"}]}
What I'm trying to do is for each person to get all the tasks and only the last comment, comment_idone if the comment exists , and save it in the same array as the task
{name: John, id: 1, task =[ {tname: "Get milk", tid: "1" comment: " Wich one", commentid: 1 },{tname: "Play cs", tid: "2" comment: " When?", commentid: 2 }]}
The problem I encountered is that I do not know how to do this. I tried adding it to array_map, but it accidentally stores comments so that it doesn't work.