By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,949 Members | 1,869 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,949 IT Pros & Developers. It's quick & easy.

Nesting foreach loops

100+
P: 348
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($reports as $report)
{
$final[] = $report;

foreach($buttons 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
Sep 7 '08 #1
Share this Question
Share on Google+
2 Replies


100+
P: 348
fjm
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
Sep 7 '08 #2

Dormilich
Expert Mod 5K+
P: 8,639
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
Sep 7 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.