Jump to content

every third loop add a break


denoteone

Recommended Posts

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}.'';

							}

 

Link to comment
https://forums.phpfreaks.com/topic/196157-every-third-loop-add-a-break/
Share on other sites

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{}.'';

							}

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.