sprintf Posted June 19, 2011 Share Posted June 19, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/239782-threaded-text-from-mysql/ Share on other sites More sharing options...
AMcHarg Posted June 21, 2011 Share Posted June 21, 2011 You need to write a recursive function. Quote Link to comment https://forums.phpfreaks.com/topic/239782-threaded-text-from-mysql/#findComment-1232801 Share on other sites More sharing options...
sprintf Posted June 21, 2011 Author Share Posted June 21, 2011 Thanks, how would I go about doing this? I know how it should work, but not how to program it. Any help appreciated! ^^ Quote Link to comment https://forums.phpfreaks.com/topic/239782-threaded-text-from-mysql/#findComment-1232925 Share on other sites More sharing options...
AMcHarg Posted June 22, 2011 Share Posted June 22, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/239782-threaded-text-from-mysql/#findComment-1233224 Share on other sites More sharing options...
sprintf Posted June 22, 2011 Author Share Posted June 22, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/239782-threaded-text-from-mysql/#findComment-1233426 Share on other sites More sharing options...
AMcHarg Posted June 23, 2011 Share Posted June 23, 2011 I think some clarification is in order. 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. Quote Link to comment https://forums.phpfreaks.com/topic/239782-threaded-text-from-mysql/#findComment-1233738 Share on other sites More sharing options...
sprintf Posted June 23, 2011 Author Share Posted June 23, 2011 Thanks again for clarifying; I have this figured out now. Quote Link to comment https://forums.phpfreaks.com/topic/239782-threaded-text-from-mysql/#findComment-1233873 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.