Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6776903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:05:03+00:00 2026-05-26T16:05:03+00:00

I created a comments system in my app which is now attached to my

  • 0

I created a comments system in my app which is now attached to my articles->view. One of the features of the comments controller is to be able to allow users to respond to a comment (one level) and being able to create many children comments for a particular comment. Everything is connected thru parent_id.

Right now, when I open an article I use foreach $comments as $comment to retrieve all comments related to the article and in articles_controller.php I assign the $comments variable to find all comments related to the article.

What is the best way to get the children comments of $comment and display it on my view?

The way I am approaching it is to have a function that returns a list of children comments based on $comments->id and display it under $comment per figure below… This seems to be time consuming as a call will be made to this function(ie. childrenComments($id)) for each comment and can definitely bog down the server.

Does anyone know of a better way to use the parent_id relation and save some CPU time?

Example - "Children" comment is manually added at this time

thank you all for your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:05:04+00:00Added an answer on May 26, 2026 at 4:05 pm

    I could not figure out how to do this, maybe the right way, so I opted to use elements to accomplish this. Basically, at each iteration of foreach $comments, I make a call to childrenComments element to check if there are children comments for the current $comment['Comment']['id']. If there are comments, I use for each to display each comment below its parent comment. See some code below….

    Code in articles_controller.php

    function childrenComments($id = null){
        $comments = $this->Article->Comment->find(
            'all',
            array(
                'fields' => array(
                    'Comment.id',
                    'Comment.comment',
                    'User.username',
                    'User.image'
                ),
                'conditions' => array(
                    'Comment.parent_id =' => $id,
                    'Comment.approved =' => 1
                )
            )
        );
        return $comments;
    }
    

    Element childrenComments.ctp

    <div style="width:100%;">
    <div>
        <?php
        $childrenComments = $this->requestAction('/articles/childrenComments/'.$id);
        foreach ($childrenComments as $childrenComment){
        ?>
        <table cellpadding=0 cellspacing=0>
            <tr>
                <td>
                    <img style="width:30px;margin-right:5px" src="<?php echo $childrenComment['User']['image'];?>">
                </td>
                <td width=100% style="padding-left:10px;">
                    <?php
                        echo $childrenComment['Comment']['comment'];
                    ?>
                </td>
            </tr>
        </table>
        <?php
            }
        ?>
    </div>
    

    Code in my /articles/view.ctp

    <?php 
         foreach($comments as $comment){
     ...
     //Code are placed here to display each parent comment
         ...
    
             //This will iterate and display all children comments
             echo $this->element('childrenComments',array('id' => $comment['Comment']['id']));
         }
    ?>
    

    And the result is:
    Actual result

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a membership system and only signed in users can enter comments.
I'm wondering, if its possible to use macros (PERSONAL.XLSB) file created on one system
I have created a data access layer in my web app which uses ObjectDataSource
I would like to create a pagination system for comments in my website.So far,
i already created a table for comments but i want to add the feature
I have created a user control to handle adding comments to certain business entities,
I've created a list using the SharePoint (MOSS 2007) Issue Tracking List. A Comments
I'm trying to create a Comments section in my wiki. There's one solution on
Which is the correct doxygen format for PHP comments? #1 /** Create HTML for
Windows Phone 7.5 app. Designer has created Home.xaml in Blend that has 3 images(menu

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.