/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] for each help

Hey, I have a script to display images in an ordered list and then styled to make it look nice. The only problem is that this uses the css property float and I am just using div tags so it messes that up. I was wondering how I would go about creating a foreach statement to create a table 4 columns wide? If I am not being clear just let me know and I will try to explain. Thanks for any help.

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@ss1289Mar 06.2008 — I'm not sure if I understand right but you could wrap each element in the foreach loop with <td> tags.
Copy linkTweet thisAlerts:
@EJMAES1973authorMar 06.2008 — Right, that is pretty much what I am wanting to do, but I also only want 4 images per row and that is the part I don't know how to add to the foreach loop
Copy linkTweet thisAlerts:
@tfk11Mar 06.2008 — You'll need two loops for that.

[code=php]
//$items = your array of items
$num_cols = 4;
echo '<table>';
for( $i = 0, $max = count($items) / $num_cols; $i < $max; ++$i ) {
echo '<tr>';
for( $i2 = 0; $i2 < $num_cols; ++$i2 ) {
$current_item = $i * $num_cols + $i2;
if( isset($items[$current_item]) ) {
echo '<td>', $items[$current_item], '</td>';
} else {
echo '<td>&nbsp</td>';
}
}
echo '</tr>';
}
echo '</table>';
[/code]


Note that the code above is untested but hopefully it helps.
Copy linkTweet thisAlerts:
@ss1289Mar 06.2008 — Right, that is pretty much what I am wanting to do, but I also only want 4 images per row and that is the part I don't know how to add to the foreach loop[/QUOTE]

so you only want to go through the foreach loop 4 times?

Use a counter, check when it reached 4, then use "break;" to break out of the loop.
Copy linkTweet thisAlerts:
@EJMAES1973authorMar 09.2008 — Note that the code above is untested but hopefully it helps.[/QUOTE]


It worked no worries. I'm surprised it did though. Not because of your code, but because I combined two different sets of code and I know pretty much nothing about for statements. Thank you so much for the help. Does anyone know a good place to learn about for statements?
Copy linkTweet thisAlerts:
@tfk11Mar 11.2008 — Glad the code helped.

Everything you need to know about for statements can be found in the php manual under control structures.
Copy linkTweet thisAlerts:
@EJMAES1973authorMar 11.2008 — alright thanx. I was able to understand that code enough to use it for other things so I think I am getting the hang of it. Thanx again
×

Success!

Help @EJMAES1973 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.9,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...