Jerry48225 Posted March 28, 2017 Share Posted March 28, 2017 I have an array of comments for a webpage that gets generated into static HTML with a cronjob. Basically, there is an unordered array which is then sorted by date, and then sorted into a new array where replies are always then sorted under what they're replying to with the following code. I finally got it to work and put everything in the correct order with this code; The nested if statements fix the problem of replies to replies not showing up before, but this only works if I have a specific loop for each possible nested level. The outer loop runs for each array value containing a comment which isn't a reply, and then the inner loops check to see if the last added array value has a reply, if they do then it adds that reply and checks again, etc. The array is already sorted via the date before this code so there won't be any issues relating to the time. $properorder = array(); foreach ($onlycomments as $comment){ $properorder[] = $comment; $CheckAgainstComment = end($properorder); foreach ($onlyreplies as $reply){ if ($reply['replytocomment'] == $CheckAgainstComment['commentid']){ $properorder[] = $reply; $CheckAgainstReply1 = end($properorder); foreach ($onlyreplies as $reply){ if ($reply['replytocomment'] == $CheckAgainstReply1['commentid']){ $properorder[] = $reply; $CheckAgainstReply2 = end($properorder); foreach ($onlyreplies as $reply){ if ($reply['replytocomment'] == $CheckAgainstReply2['commentid']){ $properorder[] = $reply; $CheckAgainstReply3 = end($properorder); foreach ($onlyreplies as $reply){ if ($reply['replytocomment'] == $CheckAgainstReply3['commentid']){ $properorder[] = $reply; $CheckAgainstReply4 = end($properorder); } } } } } } } } } For example, if there was a comment that had a reply to a reply to a reply to a reply to a reply I would need to loop for each level. I am fairly new to PHP and this code is kind of a mess and I am looking to somehow make it so I don't have to check for every amount of possible replies there could be (infinite); Like some kind of while loop that breaks out or continues to the next level automatically and increments the $CheckAgainstReply1 to $CheckAgainstReply2 automatically. I've tried doing this various ways but they never work and end up either making the comment orders worse, make some comments not appear, or even cause infinite loops which makes me need to restart php5-fpm. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted March 28, 2017 Share Posted March 28, 2017 Huh? Quote Link to comment Share on other sites More sharing options...
Jerry48225 Posted March 28, 2017 Author Share Posted March 28, 2017 Huh? Thanks for the help Quote Link to comment Share on other sites More sharing options...
ginerjm Posted March 28, 2017 Share Posted March 28, 2017 (edited) I think you read my last post incorrectly. I think you need to give us some help. If you are already concerned about the complexity of this solution you are a pretty smart person. You are building a house of cards that needs tinkering whenever something new comes along. That is because you are trying to build a 'data atructure' that is not easily worked with and therefore should be avoided. Where is all this data coming from? A db I hope. That would be terrific, assuming that it is properly normalized. What are you trying to produce out of all the current manipulations you are doing? A viewable html table maybe? A simple list of things under individual headings? A text document broken into pages? What? You need to write a proper query that joins your properly normalized tables into a single set of rows that solve your dilemma. Then you simply browse thru the single array (query result) and generate your output. This also solves your future problems by not having to make tweaks to loop after loop. So - I think you need to tell us what your proposed output is to be. Then you should give us a simple description of your table layouts so we can see what you are working with at the onset. If it all looks good/workable then we'll let you move on with a query design and your processing loop. Edited March 28, 2017 by ginerjm Quote Link to comment Share on other sites More sharing options...
Kosonome Posted March 29, 2017 Share Posted March 29, 2017 If I understood right, you have an array with a reply, and that reply has another array with another reply, and so on. This kind of problems is solved with recursive functions or just recursion. Maybe you should try learn about that. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted March 29, 2017 Share Posted March 29, 2017 No. He needs to learn how to properly store data. Piling up tons of complex workarounds for simple problems is never a good idea. Quote Link to comment 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.