• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
nesting mysql results, need help passing the array to my view

#2
[eluser]Taff[/eluser]
I had a similar issue a few days back. Unfortunately nobody could help me on the forums.
This is what I ended up doing:
//Get the list into the controller
Code:
$data['query'] = $this->model->get_list($this->user_id);
//copy it to avoid issues and get convert the result into an array
$test = $data['query']->result_array();
//find out how long it is
$len = sizeof($test);
//Loop through test
for($i=0;$i<$len;$i++){
//create a temp vaariable grabbing the items from my second table with a value equal to list_id
$tmp=$this->model->get_todo_items($test[$i]['list_id'],$this->user_id);
//assign a new element to the array called sublink
  $test[$i]['sublink']=$tmp->result_array();
}
//Reassign test back to data['query'] with our modified "query" array which now contains a new element called sublink.
$data['query']=$test;

which will output something like this:
Code:
array(8) {
    ["list_id"]=>
    string(1) "2"
    ["title"]=>
    string(23) "Second to do list title"
    ["sublink"]=>
    array(2) {
      [0]=>
      array(11) {
        ["item_id"]=>
        string(1) "3"
        ["list_id"]=>
        string(1) "2"
        ["responsible"]=>
        string(4) "Taff"
        ["title"]=>
        string(29) "Second title for to do list 1"
      }
      [1]=>
      array(11) {
       ["item_id"]=>
        string(1) "4"
        ["list_id"]=>
        string(1) "2"
        ["responsible"]=>
        string(4) "Taff"
        ["title"]=>
        string(29) "Second title for to do list 2"
      }
}

Hope I understood your question correctly, and that this helps.

Taff


Messages In This Thread
nesting mysql results, need help passing the array to my view - by El Forum - 05-16-2008, 12:36 PM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.