Note that there are some explanatory texts on larger screens.

plurals
  1. POSorting and storing array returned by a method as parent and child data for displaying nested/multi level comments in codeigniter 2.0
    text
    copied!<p>Hey guys I'm trying to learn codeigniter, but once again I'm STUCK and I seek help (as usual :P )</p> <p><code>What I need to do?</code><br> -> I need to get the data related to a article from the database along with other stuff like the tags for the article and all the <code>comments</code>. I'm thinking of keeping single level nested comments for the article.</p> <p>Well I'm done with the tag part [link to the answer which helped me with the same : <a href="https://stackoverflow.com/questions/5987951/returning-and-using-multidimensional-array-of-records-from-database-in-codeignite">Returning and using multidimensional array of records from database in CodeIgniter 2.0</a> ] but the <code>comment part</code> is driving me nuts.<br> Well to get started here is my comments table</p> <pre>Comments +---------------+-------------+ | Field | Type | +---------------+-------------+ | commentId | int(10) | | PostId | int(10) | | author | varchar(30) | | email | varchar(30) | | url | varchar(50) | | date | datetime | | comment | text | | parent | int(10) | +---------------+-------------+ </pre> <p>I'm using the parent field to keep a track of the parent for a nested child comment. By default the value is 0 which means it the parent. Child comment will have the commentid of its parent comment </p> <pre> public function getPost($postName = NULL , $year = NULL, $month = NULL ){ if($postName != NULL && $year != NULL && $month != NULL){ //single post $this->load->model('comment_model'); $this->db->where('postName',$postName); $this->db->where('year(date)',$year); $this->db->where('month(date)',$month); $q = $this->db->get('mstack_Post'); if($q->num_rows()>0){ $post = $q->result(); foreach ($post as &$p) { $p->tags = $this->getAllTags($p->postId); /* getting the comments */ $com = $this->comment_model->getComments($p->postId); /*echo count($com).' is the total count'; output= 4 */ foreach ($com as &$c) { /* trying to filter the comment. but all I get is 1 comment as the output*/ if($c->parent==0){ $p->comments->parentComment = $c; }elseif($c->commentId==$c->parent){ $p->comments->childComment = $c; } } } return $post; }else{ return array(); } } } </pre> <p>Any help will surely be appreciated. If you have any other technique /idea to display multi level comments then do let me know. :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload