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 have 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;
}
What I get is:
我得到的是:
{name: John, id: 1, task =[ {tname: "Get milk", tid: "1"},{tname: "Play cs", tid: "2"}]}
What I am trying to do is for each person get all tasks and only 1 latest comment, comment_id
if comment exists and store it in same array as the task
我想要做的是每个人获得所有任务,只有1个最新评论,如果评论存在则为comment_id,并将其存储在与任务相同的数组中
{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 have faced that I don't have any clue on how to do that.. I have tried adding it to array_map
but it stores comments randomly it did not seam to work
我遇到的问题我没有任何关于如何做到这一点的线索..我已经尝试将它添加到array_map但它随机存储注释它没有接缝工作
1 个解决方案
#1
0
please use jquery to catch json data--it is easy way to catch
请使用jquery来捕获json数据 - 这是很容易捕获的方法
$( document ).ready(function() {
$.getJSON("YOUR_PHP_LINK",function(data)
{
var tb = $("#tab");
$.each(data,function(i,value)
{
tb.append("<tr><td>Name: " + value.name + "</td><td>ID: " + value.id+ " </td></tr>");
});
});
});
#1
0
please use jquery to catch json data--it is easy way to catch
请使用jquery来捕获json数据 - 这是很容易捕获的方法
$( document ).ready(function() {
$.getJSON("YOUR_PHP_LINK",function(data)
{
var tb = $("#tab");
$.each(data,function(i,value)
{
tb.append("<tr><td>Name: " + value.name + "</td><td>ID: " + value.id+ " </td></tr>");
});
});
});