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 4604402
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:17:27+00:00 2026-05-22T00:17:27+00:00

I have a callback function for another function that loops through the comments of

  • 0

I have a callback function for another function that loops through the comments of an article in my blog. I now tried to switch to “threaded/nested” comments and therefore extended the callback function. So far everything works, but i can’t get rid of the feeling, that i didn’t write it according to best php-practice (and performance).

I use a css framework and have to do some math for the .span-xy classes i assign to the single comments. I start with an input value from a global constant and output eg. span-12 for a parent comment. Then i have to reduce/rise the value per +/- (int) 1 for every level of nesting. So i came to building arrays, counting them, iterating through and building temp arrays for every comment.

Question:
Is there an easier way to get around this?

<!-- This is the final html mark-up output: List of comments (threaded/nested) -->
<ul>
    <li id="1" class="push-<?php echo $push; ?> span-<?php echo $span; ?>">comment - parent</li>
    <li id="2">comment - child of #1
        <ul class="children">
            <li id="3">comment - child of #2
            <li id="4">comment - child of #2
                <ul class="children">
                    <li id="5">comment - child of #4</li>
                </ul>
            <li id="6">comment - child of #2</li>
        </ul>
        <li id="7">comment - child of #2
            <ul class="children">
                <li id="8">comment - child of #7</li>
                <li id="9">comment - child of #7</li>
            </ul>
        </li>
    </li>
</ul>

<?php
// This is my callback function
function comment_list_cb( $comment, $args, $depth )
{
    // retrieve the data from the globals or make them available
    $GLOBALS['comment'] = $comment;
    global $post;

    static $width = MY_GLOBAL_WIDTH_CONSTANT;
    static $ancestors = null;

    // is Child/Parent comment
    $parent = (int) $comment->comment_parent; // retrieve the ID of the parent

    $is_child = false;
    if ( $parent > (int) 0 ) // if we got a parent
    {
        $is_child = true;

        if ( ! (array) $ancestors )
            $ancestors = array();

        if ( ! array_key_exists( $parent, $ancestors ) )
        {
            $ancestors[$parent] = get_comment_ID();
        }
        else 
        {
            foreach ( $ancestors as $parent_id => $child_id )
            {
                if ( $parent_id == $parent )
                {
                    $ancestors_temp[$parent_id] = $child_id;
                    break;
                }

                $ancestors_temp[$parent_id] = $child_id;
            }
            $ancestors = $ancestors_temp;
        }

        $parent_counter = count( $ancestors );
        $span = $width - (int) $parent_counter;
    }
    else 
    {
        $ancestors = $parent_counter = null;
        $span = MY_GLOBAL_WIDTH_CONSTANT;
    }

$span_txt = $span - (int) 2; // reduce per `2` because of the span-2 class at the avatar element

    // now: build the classes
    $push = $parent_counter != (int) 0 ? 'push-1' : '';
    $child = $is_child === true ? ' child ' : '';
    $list = comment_class( 'span-'.$span.' '.$push.' append-bottom last hreview comment-'.get_comment_ID().' '.$microid, get_comment_ID(), $post->ID, false );

    ?>
    <!-- build the comment -->
    <li <?php echo $list; ?>>
        <div id="<?php get_comment_ID(); ?>">
            <span class="comment-avatar span-2"><!-- display avatar img - width is span-2 --></span>
            <span class="comment-meta span-<?php echo $span_txt; ?> last"><!-- display meta data like timestamp, etc. --></span>
            <span class="comment-text span-<?php echo $span_txt; ?> last"><!-- display message --></span>
        </div>
    </li>
    <?php
}
  • 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-22T00:17:28+00:00Added an answer on May 22, 2026 at 12:17 am

    Just some general comments after a quick scan. It deals with the coding, irrespective of functionality.

    $GLOBALS['comment'] = $comment;
    

    Why are you putting this in global scope? This can overwrite an existing global variable. Passing by reference may be more appropriate here.

    static $width = MY_GLOBAL_WIDTH_CONSTANT;
    

    Why is this static? Value is never changed so there’s no need to retain.

    if ( $parent > (int) 0 )
    [...]
    $span_txt = $span - (int) 2;
    [...]
    $push = $parent_counter != (int) 0 ? 'push-1' : '';
    

    No need to cast int literal to int. If you want int comparison, it’s the variable you should be casting.

    if ( ! (array) $ancestors )
        $ancestors = array();
    

    If empty array, make empty array? Just do !isset($ancestors)

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

Sidebar

Related Questions

There is a function that triggers a callback when finished; I call this function
I have some code that uses a form.submit() to open up another window when
Here I create a class in JAVA in which I have function (callback) which
To implement a callback function from the native code to Java code, I have
I have built a standalone app version of a project that until now was
I have quite a large IF statement which needs to update another form element
I have a C++ helper class that I use with objective-C. I would like
Does jQuery have a method to determine if an argument passed to function is
Normally, I would use a callback function passed to the 'jQuery.getJSON' function to do
Expanding on this question , it looks like I did not provide enough detail.

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.