user1080247
user1080247

Reputation: 1166

how to control this foreach loop

i use this foreach to build youtube video gallery but i want to display 3 videos for each line..so how to specify to loop 3 times and then move to the next line and loop 3 times... i dont need to build all loops in one line.. thanks for helping

table id="tblThumbsLayout" cellpadding="0" cellspacing="6">

<tr>

   <?php  foreach ($vcats as $vcat) { ?>

<td class="tdImg">
    <div>

      <?php  echo '<iframe class="youtube-player" type="text/html" width="  200 " height=" 100 " src="http://www.youtube.com/embed/' . $vcat['Video']['link'] . '"></iframe>' ;?>
        <div class="text"><?php echo  '<b>'.  $vcat['Video']['title'].'</b>' ; ?></div>
    </div>
</td>
<?php  } ?>
</tr>
</table>

Upvotes: 0

Views: 589

Answers (4)

joe92
joe92

Reputation: 643

Keep a count of the loop iterations. Then using the modulus operator, check whether this iteration divided by 3 has a remainder of 0. If it does, then add a break line or new table row to move onto the next line.

Like so:

<table id="tblThumbsLayout" cellpadding="0" cellspacing="6">

<tr>

   <?php  
       $counter = 1;
       foreach ($vcats as $vcat) {
   ?>

<td class="tdImg">
    <div>

      <?php  echo '<iframe class="youtube-player" type="text/html" width="  200 " height=" 100 " src="http://www.youtube.com/embed/' . $vcat['Video']['link'] . '"></iframe>' ;?>
        <div class="text"><?php echo  '<b>'.  $vcat['Video']['title'].'</b>' ; ?></div>
    </div>
</td>
<?php
       if($counter % 3 == 0){
          echo '<br/>';
        }
       ++$counter;
    }
?>
</tr>
</table>

Upvotes: 0

Grant
Grant

Reputation: 2441

I would add a div id=video to each item and in the style sheet for that div id #video use display: inline;

Then set the width of the div to allow 3 per row.

This way you don't have to worry too much about the loop.

Upvotes: 1

J&#252;rgen Paul
J&#252;rgen Paul

Reputation: 14997

<table id="tblThumbsLayout" cellpadding="0" cellspacing="6">

<tr>
   <?php $counter = 1; $last = count($vcats)-1; ?>
   <?php  foreach ($vcats as $vcat) { ?>

<td class="tdImg">
    <div>

      <?php  echo '<iframe class="youtube-player" type="text/html" width="  200 " height=" 100 " src="http://www.youtube.com/embed/' . $vcat['Video']['link'] . '"></iframe>' ;?>
        <div class="text"><?php echo  '<b>'.  $vcat['Video']['title'].'</b>' ; ?></div>
    </div>
    <?php if($counter%3==0 && $counter != $last): ?>
        <br>
    <?php $counter++;  ?>
    <?php endif; ?>
</td>
<?php  } ?>
</tr>
</table>

Upvotes: 0

GoSmash
GoSmash

Reputation: 1108

Try this:

   <?php  
        $i = 1; 
        foreach ($vcats as $vcat) { 
        if($i%3 == 0){
           echo "</tr><tr>";
        }

   ?>

    <td class="tdImg">
        <div>

          <?php  echo '<iframe class="youtube-player" type="text/html" width="  200 " height=" 100 " src="http://www.youtube.com/embed/' . $vcat['Video']['link'] . '"></iframe>' ;?>
            <div class="text"><?php echo  '<b>'.  $vcat['Video']['title'].'</b>' ; ?></div>
        </div>
    </td>

    <?php $i++; } ?>

Upvotes: 1

Related Questions