Jump to content

Recommended Posts

Hi, I'm new here, so don't be hard on me, and I'm also a php noob^^.

 

I have data in a MySQL table, (called info), like this:

______________

id  |  text | parent |

1  | a      | NULL  |

2  | b      | 1        |

3  | c      | 1        |

4  | d      | 2        |

------------------------

(id is auto incrementing)

 

I want to display this data in PHP, like this:

>a (ID 1)

>>b(ID 2)

>>>d (ID 4, parent 2)

>>c (ID 3)

 

This is sort of like a threaded comment system in PHP.

If you can help me with this, thanks in advance.

I have attempted different methods, but I can't seem to be able to get them to work either way.

 

Link to comment
https://forums.phpfreaks.com/topic/239782-threaded-text-from-mysql/
Share on other sites

Have you written some pseudocode, or any php code? If you have then you should post. :)

 

If I were you I would add another field into your database, entitled "level". In your case id1 would be level 1, id2&3 would be level 2 and id4 would be level 3, and so on.

 

Loop through the top levels and each time you do so, call a function. The function will then loop through the first of the next level and output it, see if it has any children and output them, and it will keep repeating the process by calling itself. It basically keeps calling itself until it has output the whole tree structure.

 

You might find some examples on Google if you search for "php tree structure", or the like.

 

If I add a 'level' column, could I use a  function like this?:

function getreplies($id, $parent, $level){
$mainquery = SELECT * FROM texts WHERE id=$id AND parent=NULL;
$commentid  = $row['id'];
$text = $row['text'];
echo $text, $commentid;

$replyquery = SELECT * FROM texts WHERE parent=$id
if(mysql_num_rows($replyquery) != 0){
$replyid = $row['id'];
$replytext  = $row['text'];
$level = str_repeat($row['level'], $row['level']); //repeat level, [level] times
echo $level." ".$replytext;
}
    }

This, as you can see, is in pseudocode, not sure how to do it in PHP fully; I do not think the function is recursive, but instead does 1 query, and checks for replies with another query; then prints them out.

Does this look good?

 

 

I think some clarification is in order.  8)

 

Your original post implies that there will be more than two levels of information, like this (using groceries as an example to ease its explanation, and have marked numbers to show the $level) ...

 

(1) Groceries

    (2) Dairy

          (3) Milk

          (3) Butter

              (4) Low Fat Butter

              (4) Full Fat Butter

          (3) Eggs

    (2) Fruit

          (3) Apples

          (3) Bananas

          (3) Oranges

              (4) Large Oranges

              (4) Small Oranges

 

If the above is what you are looking for then your code will not do it. Coding a loop into a loop will only work if you have two levels. If you have three levels then using your method you would need to do a loop in a loop in a loop, and so on. This method still works, but it means you are limited to the number of levels you can have.

 

As a sidenote: when I mentioned pseudocode it's a pretty major part of planning a complicated piece of code. Most on here will just write code because the pieces of code are small and they are experts at it, but if you can't get your head around how to program something you should first try to get your head around the logic of what needs to happen (in plain English). Your example isn't pseudocode. An example would be:

 

- Loop through the level 1 items
- For each item, output it and call a function to get it's children
- The function should output the first child and then call itself, increasing the level by 1 to get the grandchildren.
- The process is repeated recursively, but the function continuing to call itself until it has exhausted all the rows.

 

If only two levels then your way works, as follows:

- Loop through level 1 items
- For each item loop through it's children

 

Using your method, for three levels, your way works as follows:

- Loop through level 1 items
- For each item loop through it's children
- For each item loop through it's children

But, as stated previously, this method will limit the number of levels.

 

8)

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.