Reputation: 1816
I am trying to figure out this thing in codeigniter, but because I am a beginner, I am having some trouble finding the solution for it.
I have a table named tasks
and another table named progress
. So a number of tasks are inserted in tasks
table and progress for those tasks are inserted in progress
table. I am using different table to track progress because I may need to track down the progress history for each tasks later on.
$all_tasks = $this->task_m->get_by(array('user_id'=> $this->session->userdata('id')));
foreach ($all_tasks as $task) {
$progress = $this->progress_m->get_by(array('task_id'=> $task->id));
};
So what I am trying to do is add the entries of both the $task
and $progress
to an array so that I can loop through it and display the combined result for a task at a time.
There are two entries in tasks
table and the value of $tasks
is :
array(2) {
[0]=>
object(stdClass)#24 (8) {
["id"]=>
string(1) "6"
["user_id"]=>
string(1) "2"
["name"]=>
string(28) "Sample task to do sometihing"
["description"]=>
string(28) "Sample task to do sometihing"
["priority"]=>
string(4) "high"
["status"]=>
string(7) "pending"
["created"]=>
string(19) "2014-02-27 23:26:09"
["modified"]=>
string(19) "2014-02-27 23:26:09"
}
[1]=>
object(stdClass)#25 (8) {
["id"]=>
string(1) "7"
["user_id"]=>
string(1) "2"
["name"]=>
string(13) "Sample task 2"
["description"]=>
string(11) "Sample desc"
["priority"]=>
string(1) "1"
["status"]=>
string(0) ""
["created"]=>
string(19) "2014-02-28 00:20:17"
["modified"]=>
string(19) "2014-02-28 00:20:17"
}
}
There are two entries in progress
table and the value for each $progress
is :
array(1) {
[0]=>
object(stdClass)#26 (6) {
["id"]=>
string(1) "1"
["task_id"]=>
string(1) "6"
["percentage"]=>
string(2) "68"
["description"]=>
string(17) "About to complete"
["created"]=>
string(19) "2014-02-18 00:00:00"
["modified"]=>
string(19) "2014-02-12 00:00:00"
}
}
array(1) {
[0]=>
object(stdClass)#27 (6) {
["id"]=>
string(1) "2"
["task_id"]=>
string(1) "7"
["percentage"]=>
string(2) "56"
["description"]=>
string(6) "Sample"
["created"]=>
string(19) "2014-02-05 00:00:00"
["modified"]=>
string(19) "2014-02-14 00:00:00"
}
}
Upvotes: 0
Views: 858
Reputation: 932
You're almost there:
$all_tasks = $this->task_m->get_by(array('user_id'=> $this->session->userdata('id')));
foreach ($all_tasks as $task) {
$task->progress_list = $this->progress_m->get_by(array('task_id'=> $task->id));
};
Upvotes: 1
Reputation: 1924
Why not just use ActiveRecord to join the two tables, run the query and then you have one result object with all the info? I am assuming they share a common key correct? If not, you may want to re-think the way your DB is laid out. If you don't want to use ActiveRecord you could write the query in standard MySQL and use the query() method.
Upvotes: 0