Jump to content

Converting nested loops into a single loop


Jerry48225
 Share

Recommended Posts

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.

 

Link to comment
Share on other sites

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 by ginerjm
Link to comment
Share on other sites

This thread is more than a year old.

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.

 Share

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