CodeIgniter Forums
nesting mysql results, need help passing the array to my view - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: nesting mysql results, need help passing the array to my view (/showthread.php?tid=8406)



nesting mysql results, need help passing the array to my view - El Forum - 05-16-2008

[eluser]skattabrain[/eluser]
ok, still new to CI ... and object orientated design. i have no problems doing this procedurally.

creating a profile page for users ... on this page i load their details, then any comments made about them. i also want to include a list of replies for each on those comments.

i pass the data to my view like this ...

Code:
$this->load->view('profile.php', $data);

so i fetch my client, then all comments made on them, here's the code to fetch the comments ...

Code:
$comments = $this->Profile_data->getCoComments($clientID);
$data['comments'] = $comments;

now, i get the replies to each of these comments ...
Code:
if ($comments->num_rows() > 0)
{
foreach ($comments->result() as $comment)
$replies = $this->Profile_data->getReplies($comment->commentID);
}

the trouble i'm having is attaching this new reply array to the exisiting comment array ... so in my view i can simply loop through each comment, and while in a comment, i check for replies and loop through them before moving to my next comment. not really sure how to do this ... again, i've done this procedurally forever now ...


nesting mysql results, need help passing the array to my view - El Forum - 05-16-2008

[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


nesting mysql results, need help passing the array to my view - El Forum - 05-16-2008

[eluser]Pascal Kriete[/eluser]
Taff has it spot on. Just to add my 2c though, returning the result_array from your model makes this a lot easier. Also, if you're using PHP5 you can do the loop very easily by using a reference in the foreach.
Code:
$comments = $this->Profile_data->getCoComments($clientID);

// No longer need the if, since we know it's an array.
foreach ($comments as &$comment)
{
    $comment['reply'] = $this->Profile_data->getReplies($comment->commentID);
}

$data['comments'] = $comments;

The same works in PHP4, but you have to do foreach($comments as $key => $comment) and then use $comments[$key]['reply'] similar to what Taff did.


nesting mysql results, need help passing the array to my view - El Forum - 05-16-2008

[eluser]skattabrain[/eluser]
thanks for your insights ... thank you very much.

this is what i did shortly after my post ...

Code:
$comments = $this->Profile_data->getCoComments($clientID);
$data['comments'] = $comments;
            
if ($comments->num_rows() > 0)
{
  foreach ($comments->result() as $comment)
   {
     $replies = $this->Profile_data->getReplies($comment->commentID);
     $data["replies"][$comment->commentID] = $replies;
   }
}

then in my view inside the comment loop, i do this ...

Code:
if($replies[$comment->commentID]->num_rows() > 0):
foreach($replies[$comment->commentID]->result() as $reply):
etc ...


nesting mysql results, need help passing the array to my view - El Forum - 08-17-2011

[eluser]Miguel Diaz[/eluser]
One question I am trying to do the same thing but I am still new at this. I was trying to use my database model with this but it do not work. Can some one help me please

This is my model
Code:
function getReply()
    {
        $data = array();
        $q = $this->db->query("select * from replies");
        if($q->num_rows() > 0) {
            foreach($q->result() as $row){
                $data[] = $row;
            }
            $q->free_result();      
        }
        return $data;
    }

and my controller
Code:
$comments = $this->data_model->getComments($commentID);

        // No longer need the if, since we know it's an array.
        foreach ($comments as &$comment)
        {
            $comment['reply'] = $this->data_model->getReply($comment->commentID);
        }
        
        $op['comments'] = $comments;          
        
        $this->layouts->view('articulos-template', $op);

View
Code:
&lt;?php foreach($comments as $row) :?&gt;
<p>&lt;?=$row->comment;?&gt;<p>
<h2>Reply</h2>
     &lt;?php foreach($reply as $rowReply) :?&gt;
     <p>&lt;?=$rowReply->reply;?&gt;<p>
     &lt;?php endforeach; ?&gt;
&lt;?php endforeach; ?&gt;

Can someone help me and let me know what i am doing wrong