【问题标题】:how to control this foreach loop如何控制这个 foreach 循环
【发布时间】:2012-05-01 00:22:57
【问题描述】:

我使用这个 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>

【问题讨论】:

  • 所有建议的答案:不要忘记检查它是否不是最后一行

标签: php


【解决方案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++; } ?>

【讨论】:

  • 感谢 Ramesh,它工作得很好,我只需将 if($i%3 == 0){ 更改为 if($i%3 == 1){
  • 谢谢。对不起,我犯了一个简单的错误。
【解决方案2】:

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

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

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

【讨论】:

    【解决方案3】:
    <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>
    

    【讨论】:

      【解决方案4】:

      记录循环迭代次数。然后使用取模运算符,检查这个迭代除以 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>
      

      【讨论】:

        猜你喜欢
        • 2020-06-12
        • 1970-01-01
        • 2015-08-12
        • 1970-01-01
        • 1970-01-01
        • 2023-01-04
        • 2020-10-18
        • 1970-01-01
        • 2014-08-08
        相关资源
        最近更新 更多