1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Rep Power
    I'm trying to get my head around an algorithm that renders a nested discussion.

    I assume all the data I need is a discussion #ID, unique post ID - and the parent post's #ID ('storyid', 'postid', 'parentid').

    With that data I'd like to render a nested discussion: root posts are left-most, child posts are indented from the left.

    It just seems awfully difficult, and although I've looked at PHP code backing other web forums it's too evolved for me to understand (and usually broken up over several files).

    Any help would be greatly appreciated. Thanks.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Newark, DE, USA
    Rep Power
    as all posts are added, they get assigned a unique ID. a thread parent has no parent ID, but all responses to posts inherit the ID of the parent as its parent ID (go figure!). So you have a nicely organized database.

    when you retrieve posts, you want to get all the ones without parent IDs, as they are the thread parents. then, as you analyze and print the parents, you'll have to get all the posts whose parent IDs are that of the current parent's.

    If you had a MySQL table called "posts" with the columns "UID" and "PID" (unique & parent IDs):
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    $getParents = mysql_query( "select UID, PID from posts where PID=''");
    if( mysql_num_rows( $getParents ) <= 0 ){
    echo( "OH NO! No discussions for you!" );
    } else {
    while( list ( $UDI, $PID ) = mysql_fetch_row( $getParents ) ){
    echo "THIS PARENT HAS ID: ".$UID."<br>";
    $getReplies = mysql_query( "select UID, from posts where PID='$PID'" );
    if( mysql_num_rows( $getReplies ) <= 0 ){
    echo "OH NO! No replies for you!" );
    } else {
    while( list( $uid ) = mysql_fetch_row( $getReplies ) ){
    echo "THIS REPLY HAS PARENT ".$PID." AND HAS ID: ".$uid."<br>";
    Of course this only handles posts w/ one level of replies. You'll need some recursion to get your script to handle replies to replies (to replies to ...). I hope this helps you get started.


    [This message has been edited by cwwesley (edited October 23, 2000).]
  4. #3
  5. No Profile Picture
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2000
    Rep Power
    Simple threaded display suggestion with recusive function:
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>


    function DisplayThread($ParentID,$Level)
    if($level>=strlen($strlevel)) $strlevel = $strlevel."0";

    //get messages with parent = ParentID

    for(**each message with parent $ParentID**)
    if(**more message with parent $ParentID to do after this one**) $next=true;
    else $next=false;

    if($next) $strlevel[$level]='1';
    else $strlevel[$level]='0';

    if($strlevel[$i]=='1') **display down | **
    else **display empty **

    if($next) **display t split |-**
    else **display r split |_**

    **display the message + to nextline **

    DisplayThread($newparentID,$Level+1); //display message children

    Greetings lewi

    [This message has been edited by Lewi (edited October 23, 2000).]

Similar Threads

  1. more than 1 nested table
    By tanuja in forum Oracle Development
    Replies: 1
    Last Post: December 30th, 2003, 01:39 AM
  2. How to write nested structures to a binary file
    By Bacat in forum C Programming
    Replies: 6
    Last Post: December 29th, 2003, 12:00 PM
  3. nested query and MySQL query
    By lucipher in forum MySQL Help
    Replies: 1
    Last Post: November 19th, 2003, 10:49 AM
  4. Please help! Loading variables into a nested movie!
    By banzai_uk in forum Flash Help
    Replies: 4
    Last Post: September 5th, 2003, 01:12 PM
  5. nested threads
    By thinkninja in forum PHP Development
    Replies: 3
    Last Post: January 12th, 2003, 12:33 PM

IMN logo majestic logo threadwatch logo seochat tools logo