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

1

试试这个:

   <?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++; } ?>
于 2012-04-19T13:19:48.093 回答
1

我会为每个项目添加一个 div id=video 并在该 div id #video 的样式表中使用 display: inline;

然后将 div 的宽度设置为每行允许 3 个。

这样你就不必太担心循环了。

于 2012-04-19T13:22:43.320 回答
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 回答