almightyegg Posted September 17, 2007 Share Posted September 17, 2007 At least I think it will need a while or several.... On my message board I want the posts to be displayed like: Reply 6 - Username Reply 8 - Username Reply 7 - Username Reply 1 - Username Reply 3 - Username Reply 4 - Username Reply 5 - Username Reply 2 - Username As you can see replies can have replies and replies of replies etc.... id = post id fid = post forum rid = what post it is in reply to I had something on the idea of whiles within whiles but what I tried didn't work.... Can anyone provide a solution on how to get these posts echoes out?? Quote Link to comment https://forums.phpfreaks.com/topic/69645-solved-while-help/ Share on other sites More sharing options...
ViN86 Posted September 17, 2007 Share Posted September 17, 2007 make a function that checks if replies exist displays the replies. call it recursively to display all replies for each. a recursive function would suit you much better in this case rather than multiple WHILE loops. Quote Link to comment https://forums.phpfreaks.com/topic/69645-solved-while-help/#findComment-349939 Share on other sites More sharing options...
almightyegg Posted September 17, 2007 Author Share Posted September 17, 2007 I have no idea how to wirte a recursive function :-X I was new to functions completely only yesterday.... Quote Link to comment https://forums.phpfreaks.com/topic/69645-solved-while-help/#findComment-350005 Share on other sites More sharing options...
rarebit Posted September 17, 2007 Share Posted September 17, 2007 It's a function which calls itself. Usually used for mapping directory systems. e.g. print all all files in dir and call self if find a dir... Quote Link to comment https://forums.phpfreaks.com/topic/69645-solved-while-help/#findComment-350042 Share on other sites More sharing options...
almightyegg Posted September 17, 2007 Author Share Posted September 17, 2007 How would I write one??? Quote Link to comment https://forums.phpfreaks.com/topic/69645-solved-while-help/#findComment-350053 Share on other sites More sharing options...
GingerRobot Posted September 17, 2007 Share Posted September 17, 2007 You can make life a lot easier in the way you set up your array. The way i approach this is with a two dimensional array. For each post, the first dimension is the id of the parent post, and second is the id of the current post. You therefore set a top level id (which in the following example was 0): <?php $array = array(); $array[0][6] = 'Jim'; $array[6][8] = 'Bob'; $array[0][7] = 'Tom'; $array[0][1] = 'Dick'; $array[1][3] = 'Harry'; $array[3][4] = 'Fred'; $array[4][5] = 'George'; $array[1][2] = 'Run out out names'; function recursive($array,$index=0){ foreach($array[$index] as $k => $v){ echo "<li>Reply $k - $v</li>"; if(is_array($array[$k])){ echo '<ul>'; recursive($array,$k); echo '</ul>'; } } } echo '<ul>'; recursive($array); echo '</ul>'; ?> Of course, this isn't going to work as-is, but it might give you an idea of how these things work. Here's another example(which is largely identical to the one above, but for a breadcrumb menu): http://www.phpfreaks.com/forums/index.php/topic,156551.msg680172.html#msg680172 Again, might just help you on your way. And one further example that i wrote, which was for generating a listing of the number of files within a directory and all sub directories: http://www.phpfreaks.com/forums/index.php/topic,157389.msg684881.html#msg684881 Quote Link to comment https://forums.phpfreaks.com/topic/69645-solved-while-help/#findComment-350079 Share on other sites More sharing options...
almightyegg Posted September 17, 2007 Author Share Posted September 17, 2007 I think I can see a way of doing it, possibly.... Depends. What I would do is: 1. somehow while out the top level replies into the $array 2. Then use the $array to fetch_assoc etc.... 3. This is where I'm stuck....getting to the next lot of replies... Would I make another internal array using while and then again etc.... ??? Quote Link to comment https://forums.phpfreaks.com/topic/69645-solved-while-help/#findComment-350082 Share on other sites More sharing options...
almightyegg Posted September 17, 2007 Author Share Posted September 17, 2007 ooooh! I ran your script and was very surprised. It showed something completely different to what I thought it would...but exactly what I need. Yet there's things I still need to get my head round.... when creating the array I don't know how to while it out successfully I would still need to do several whiles to get all the data I need as there is no one thing the same for each reply that I can seach for the first stream of replies the rid is the current post, second stream rid is from one of the first stream rids, third from one of the second stream rid and so on.... Is there a way I can get it all into the array??? Quote Link to comment https://forums.phpfreaks.com/topic/69645-solved-while-help/#findComment-350093 Share on other sites More sharing options...
GingerRobot Posted September 17, 2007 Share Posted September 17, 2007 Well, in your given example, id is the id of the post, whilst rid is the parent id. Assuming you set a default on the rid field of 0, you could do something like: <?php $sql = "SELECT * FROM `forum` WHERE `fid`='$forumnumber'"; $result = mysql_query($sql) or die(mysql_error()); $array = array(); while($row = mysql_fetch_assoc($result)){ $parent_id = $row['rid']; $post_id = $row['id']; $user= $row['user']; $array[$parent_id][$post_id] = $user; } ?> That would generate the array i typed out. Quote Link to comment https://forums.phpfreaks.com/topic/69645-solved-while-help/#findComment-350097 Share on other sites More sharing options...
GingerRobot Posted September 17, 2007 Share Posted September 17, 2007 ooooh! I ran your script and was very surprised. It showed something completely different to what I thought it would...but exactly what I need. The general idea of me typing that out is that you try it! Quote Link to comment https://forums.phpfreaks.com/topic/69645-solved-while-help/#findComment-350099 Share on other sites More sharing options...
almightyegg Posted September 17, 2007 Author Share Posted September 17, 2007 Well, I read it first and thought I understood what it did, but I obviously didn't Quote Link to comment https://forums.phpfreaks.com/topic/69645-solved-while-help/#findComment-350100 Share on other sites More sharing options...
almightyegg Posted September 17, 2007 Author Share Posted September 17, 2007 I think I got it near enough working.... only problem is that it echoes: Reply 1 - $hm[title] - $hm[username] Reply 6 - $nm[title] - $nm[username] Reply 10 - $om[title] - $om[username] Reply 7 - $nm[title] - $nm[username] Reply 9 - $om[title] - $om[username] Reply 8 - $om[title] - $om[username] Reply 5 - $hm[title] - $hm[username] Reply 4 - $hm[title] - $hm[username] Reply 3 - $hm[title] - $hm[username] Reply 2 - $hm[title] - $hm[username] How do I get the variables to work???? EDIT just occurred to me you will need to know more info The replies are set in an array: $array[0][$hmid] = '$hm[title] - $hm[username]'; $hmid is working fine. The rest isn't Quote Link to comment https://forums.phpfreaks.com/topic/69645-solved-while-help/#findComment-350130 Share on other sites More sharing options...
GingerRobot Posted September 18, 2007 Share Posted September 18, 2007 The replies are set in an array: $array[0][$hmid] = '$hm[title] - $hm[username]'; Php only parses strings for variables that are contained within double quotes. If you place them inside single quotes, they are treated as litterals - that is you, litterally want to echo $var, rather than it's value. Quote Link to comment https://forums.phpfreaks.com/topic/69645-solved-while-help/#findComment-350390 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.