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

							}

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

							}

 

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.