Nesting foreach loops

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fjm
    Contributor
    • May 2007
    • 348

    Nesting foreach loops

    Hello everyone,

    I need some help and direction with this code. I am trying to have the same functionality that this forum has with regard to the allowable length of time the post (Or in my case, report) can be edited or deleted.


    Here is what I have so far:

    [PHP]foreach($report s as $report)
    {
    $final[] = $report;

    foreach($button s as $button)
    {
    if($button['report_id'] == $report['report_id'])
    {
    $final[] = "buttons";
    }
    }
    }[/PHP]
    This outer loop gets all reports and then if the post is less than 1 hour old, places an edit and delete button in the html for that particular report. (Second loop)

    I have 3 files here. The first one is the class which has the sql and returns the results. The second is the body.php file which is where I have the calls to the db and any other stuff I need. I then have the html that gets populated with the results. I simply include() the html in the body.php file and then use any variables inside the html to populate the data.

    What I need is to display the results from my foreach loops. Should I use this loop in the body.php or inside of the html file? I was thinking about leaving it inside the body.php file and just using $final inside the html file. would this correct?

    Also, I am having an issue with the $final array. I can't seem to get the button to show.

    Thanks,

    Frank
  • fjm
    Contributor
    • May 2007
    • 348

    #2
    Well, here is what I have decided to do. It works but I am not sure if this is correct or not. What I am going for here is to seperate the application code from the view.

    I decided to put the code I posted above into the html file using alternate syntax. Again, the html file is an include() in the body.php file. Would this be correct? Even though it works, I am looking for proper way of doing this.

    Thanks,

    Frank

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      what do the $button, $report and $final arrays contain? is $reports a 2D-array? what is $final supposed to contain? If you're not sure, what the arrays contain, check it with var_dump(), maybe this is giving you a hint what's wrong.

      as for the files, I'd leave the processing in body.php but this depends on what the actual files look like, both ways may suffice.

      regards

      Comment

      Working...