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); now, i get the replies to each of these comments ... Code: if ($comments->num_rows() > 0) 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); which will output something like this: Code: array(8) { 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); 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); then in my view inside the comment loop, i do this ... Code: if($replies[$comment->commentID]->num_rows() > 0): 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() and my controller Code: $comments = $this->data_model->getComments($commentID); View Code: <?php foreach($comments as $row) :?> Can someone help me and let me know what i am doing wrong |