0

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"
  }
}

2 Answers 2

1

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));
};
Sign up to request clarification or add additional context in comments.

1 Comment

This helped. Added the progress object to $task->progress_list and now I can access the progress values as $task->progress_list->percentage . Thanks
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.