Jump to content

Jerry48225

New Members
  • Posts

    2
  • Joined

  • Last visited

Jerry48225's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. 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.
×
×
  • 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.