0

この foreach を使用して youtube ビデオ ギャラリーを構築しますが、各行に 3 つのビデオを表示したい..3 回ループしてから次の行に移動して 3 回ループするように指定する方法...すべてのループを構築する必要はありません一行で..助けてくれてありがとう

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>
4

4 に答える 4

0

ループの反復回数を記録します。次に、剰余演算子を使用して、この反復を3で割った余りが0であるかどうかを確認します。余りがある場合は、ブレークラインまたは新しいテーブル行を追加して次の行に移動します。

そのようです:

<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>
于 2012-04-19T13:20:20.450 に答える
0
<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>
于 2012-04-19T13:20:22.397 に答える