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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:18:58+00:00 2026-06-13T17:18:58+00:00

I found the below threaded comments example online and the author says it works

  • 0

I found the below threaded comments example online and the author says it works very well for him. However, I’m having problems ordering the results so the threaded comments are in the right place. This is what the example gives me:

Example author says: “I use a system which is simple and doesn’t rely on recursion. I
basically store the entire thread “path” as a row field. Want to get
the entire tree structure? Just do an ORDER BY on the path column and
use some PHP code like the following for formatting:”

The Example Data

ID | Comment                      | Path
---+------------------------------+----------
0  | Comment #1                   | 00
1  | Comment #1 reply             | 00_01
2  | Comment #1 reply reply       | 00_01_02
3  | Comment #2                   | 00
4  | Comment #3                   | 00
5  | Comment #3 reply             | 00_04

The Example SQL

SELECT * FROM comments ORDER BY path

The Example PHP

while ($result = mysql_fetch_assoc($query)) {
    $nesting_depth = count(explode("_", $result['path']));
    $branch = str_repeat("--", $nesting_depth);
    echo $branch {$result['comment']}";
}

The Example Result

Comment #1
-- Comment #1 reply
---- Comment #1 reply reply
Comment #2
Comment #3
-- Comment #3 reply

I added the exact data to my MySQL database and tested it and worked as expected. However, if I change the order of the data in the table, it does NOT work. I’ll explain:

New REALISTIC Test Data

ID | Comment                      | Path
---+------------------------------+----------
0  | Comment #1                   | 00
1  | Comment #2                   | 00
2  | Comment #3                   | 00
3  | Comment #3 reply             | 00_04
4  | Comment #1 reply             | 00_01
5  | Comment #1 reply reply       | 00_01_02

Take a look at row[4] and row[5], these reply comments were the last comments added and drastically changes the order of results:

New Test Results

Comment #1
Comment #2
Comment #3
-- Comment #1 reply
---- Comment #1 reply reply
-- Comment #3 reply

This is a big problem! Is this guy talking absolute rubbish or have I done something wrong? Unless the data is in the exact same order you want to display, it will never work. Is there something simple I can do to fix the order?

  • 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-06-13T17:18:59+00:00Added an answer on June 13, 2026 at 5:18 pm

    Every “main” comment must have a path that contains a unique ID number. In your case every “main” comment has an ID of 00. If you have three of those, then there’s no way to get a reply in between.

    ID | Comment                      | Path 
    ---+------------------------------+---------- 
    0  | Comment #1                   | 00 
    1  | Comment #2                   | 00 
    2  | Comment #3                   | 00 
    4  | Comment #1 reply             | 00_01 <-- Last item
    

    The last item will alway be the last item (alphabetically). If you distinguish every “main” comment with a unique ID, then the problem is solved.

    ID | Comment                      | Path 
    ---+------------------------------+---------- 
    0  | Comment #1                   | 01
    1  | Comment #2                   | 02 
    2  | Comment #3                   | 03 
    4  | Comment #4 reply to 1 (1)    | 01_01 <- first key is parent_id, second is sequence
    5  | Comment #5 reply to 1 (2)    | 01_02 
    6  | Comment #6 reply to 4 (1)    | 04_01 
    7  | Comment #7                   | 04 
    8  | Comment #8 reply reply to 5  | 01_02_01 
    

    So when you’re going to reply to a comment all you need to do is reference their entire path, and add an indexing key at the end.

    In the above table:

    01_01 = parent id: 01 -> sequence: 01
    04_01 = parent id: 04 (so a reply to id 4) -> sequence: 01
    01_02_01 = parent_id: 01_02 (references the path of ID 5) -> sequence: 01
    

    etc.

    Then again, this is a weird construction. In my opinion an id/parent_id relation is better for these kind of things.

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

Sidebar

Related Questions

FOUND THE ANSWER MYSELF, see below... I'd like to have a very simple user
I found this orientation test code below looking for JQTouch reference material. This works
The typical header should be #!/usr/bin/env python But I found below also works when
I found a pre-written script of a class to create threaded comments, but after
I am trying to compile this piece of code found below. However, I got
Im having problems understanding the app logic to this password reset code i found
Just like the title says I am having problems combining two classes. Altough this
I'm new to javascript programming. I have found below example while practicing javascript. <html>
I found the below code in the Internet, I'm trying to understand how Linux
I wanted to be able to Pause/ Resume my NSTimer and found this below

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.