denoteone Posted March 22, 2010 Share Posted March 22, 2010 I am looping through an array and echoing the data. I want to put an if statement that checks the incremented value and if it is a multiple of three then add a break. so loop three times and on the third add a break. Loop three more times and on the 6th add a break and so on. this is what I have so far. for($i=0;$i<23;$i++){ echo ''.$products2[$i].': <input type="text" name="'.$products2[$i].'" value="">'. if ($i/3){ .'<br/>'. else {do nothing}.''; } Quote Link to comment https://forums.phpfreaks.com/topic/196157-every-third-loop-add-a-break/ Share on other sites More sharing options...
KevinM1 Posted March 22, 2010 Share Posted March 22, 2010 Use the modulus operator: if ($i % 3 == 0) { // add break } else { // process as normal } Quote Link to comment https://forums.phpfreaks.com/topic/196157-every-third-loop-add-a-break/#findComment-1030094 Share on other sites More sharing options...
denoteone Posted March 22, 2010 Author Share Posted March 22, 2010 Whatever I did I broke the page but I am not getting an error? for($i=0;$i<21;$i++){ echo ''. if ($i % 3 == 0) {.'<div style="margin-bottom:3px;text-align:right;">'.}else{ } $products1[$i].': <input type="text" name="'.$products1[$i].'" value="" size="6">'. if ($i % 3 == 0) {.'</div>'.}else{}.''; } Quote Link to comment https://forums.phpfreaks.com/topic/196157-every-third-loop-add-a-break/#findComment-1030106 Share on other sites More sharing options...
KevinM1 Posted March 22, 2010 Share Posted March 22, 2010 Whatever I did I broke the page but I am not getting an error? for($i=0;$i<21;$i++){ echo ''. if ($i % 3 == 0) {.'<div style="margin-bottom:3px;text-align:right;">'.}else{ } $products1[$i].': <input type="text" name="'.$products1[$i].'" value="" size="6">'. if ($i % 3 == 0) {.'</div>'.}else{}.''; } Try: for ($i = 0; $i < 21; ++i) { if ($i % 3 == 0) { echo '<div style="margin-bottom:3px;text-align:right;">{$products1[$i]}: <input type="text" name="{$products1[$i]}" value="" size="6"></div>'; } else { echo '{$products1[$i]}: <input type="text" name="{$products1[$i]}" value="" size="6">'; } } The {}'s in the echos force the arrays to print their values. I like it better than entering-leaving-reentering the string with the . operator. Quote Link to comment https://forums.phpfreaks.com/topic/196157-every-third-loop-add-a-break/#findComment-1030117 Share on other sites More sharing options...
denoteone Posted March 22, 2010 Author Share Posted March 22, 2010 the only problem with this is that the first $i will always have a break because it is zero. Quote Link to comment https://forums.phpfreaks.com/topic/196157-every-third-loop-add-a-break/#findComment-1030136 Share on other sites More sharing options...
denoteone Posted March 22, 2010 Author Share Posted March 22, 2010 thanks for your help Nightslyr. I used if (($i + 1) % 3 == 0 ) Quote Link to comment https://forums.phpfreaks.com/topic/196157-every-third-loop-add-a-break/#findComment-1030147 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.