LemonInflux Posted September 27, 2007 Share Posted September 27, 2007 Is there a way to create a foreach( loop, whereby every 3rd value does something different. For example, I have a file called test.txt, that contains this: a b c d e f g h i j k l m n o p q r s t u v w x y z Now, say I create a loop like: <?php $file = file_get_contents('test.txt'); $letter = explode('\n', $file); foreach($letter as $loopletter){ echo ('<td>'.$loopletter.'</td>'); } ?> Now, what if I wanted every third value to do this: echo ('<td>'.$loopletter.'</td></tr><tr>'); ; Is this possible? Quote Link to comment https://forums.phpfreaks.com/topic/70907-is-this-possible/ Share on other sites More sharing options...
HuggieBear Posted September 27, 2007 Share Posted September 27, 2007 It is, but it looks as though you want to know so you can create tabular data in columns. If so then there's loads of posts on here about it, just search these forums. Regards Huggie Quote Link to comment https://forums.phpfreaks.com/topic/70907-is-this-possible/#findComment-356446 Share on other sites More sharing options...
LemonInflux Posted September 27, 2007 Author Share Posted September 27, 2007 I just searched for that. Your post was the only one I found. Quote Link to comment https://forums.phpfreaks.com/topic/70907-is-this-possible/#findComment-356448 Share on other sites More sharing options...
rarebit Posted September 27, 2007 Share Posted September 27, 2007 Many ways: <?php $file = file_get_contents('test.txt'); $letter = explode('\n', $file); $tot = count($letter); echo "<table><tr><td></td></tr>"; for($i=0;$i<$tot;$i+3){ echo ('<tr><td>'.$letter[$i+1].'</td><td>'.$letter[$i+2].'</td><td>'.$letter[$i+2].'</td></tr>'); } echo "</table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/70907-is-this-possible/#findComment-356449 Share on other sites More sharing options...
rarebit Posted September 27, 2007 Share Posted September 27, 2007 Hug's is right, there's loads... There's even one in the phpfreaks tutorial section... Quote Link to comment https://forums.phpfreaks.com/topic/70907-is-this-possible/#findComment-356450 Share on other sites More sharing options...
AV1611 Posted September 27, 2007 Share Posted September 27, 2007 Do you want to output a table to screen, or actually output a doc? (.txt, .csv, .xls, etc.) You can do either or both... Simply use the strategy that rarebit employed: a simple counter that keeps track of every third event, but don't forget that you will have to put some closes clauses that add what is appropriate depending on the column count when the data runs out. Quote Link to comment https://forums.phpfreaks.com/topic/70907-is-this-possible/#findComment-356452 Share on other sites More sharing options...
LemonInflux Posted September 27, 2007 Author Share Posted September 27, 2007 That's fine. Topic solved. PS. Still can't find one :\ Quote Link to comment https://forums.phpfreaks.com/topic/70907-is-this-possible/#findComment-356489 Share on other sites More sharing options...
AV1611 Posted September 27, 2007 Share Posted September 27, 2007 I know I asked that same question like a year ago so I know there's at least ONE thread on it... but I'm glad you got it to work... That's what's important. Quote Link to comment https://forums.phpfreaks.com/topic/70907-is-this-possible/#findComment-356521 Share on other sites More sharing options...
Psycho Posted September 27, 2007 Share Posted September 27, 2007 @LemonInflux, People are asking for additional information because many times what a poster is asking for is not really what they are wanting. So, by explaining what you are trying to accomplish will allow us to provide a best practice solution because the process you are asking to solve might not be the best. But, a simple answer is something like this: <?php $step = 1; foreach ($arrayVar as $value) { if ($step<3) { //Do this $step++; } else { //Do alternative $step = 1; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/70907-is-this-possible/#findComment-356535 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.