如何在foreach循环中每5次迭代后定义html标记


how to define html tag after each 5 iteration in foreach loop

我只想知道如何在foreach循环中每5次迭代后定义HTML标记<br clear="all">,这是我的代码

<?php
$i=1;    
foreach($videoEntries as $data){
?>
    <div class="item-main">
        <div class="item">
        <a href="javascript:;" onclick="ratePopup(2)" title="<?php echo $data->video_name;?>">
        <div class="overlaid"></div>
        <img src="<?php echo $image_url;?>"  width="93" height="89"/>
        </a>
        </div>
        <p title="Trailer Name"><strong><?php echo $data->video_name;?></strong></p>
        <p title="Released Date"><?php echo $data->video_released_date;?></p>
    </div>
<?php 
    if($i == 5){
        echo "<br clear = 'all'>";    
    }
}
?>

所需结果或帮助明确告知

12345
<br clear="all">
678910
<br clear="all">

试试这个:

<?php
$i=0;    
foreach($videoEntries as $data){
$i++;
?>
    <div class="item-main">
        <div class="item">
        <a href="javascript:;" onclick="ratePopup(2)" title="<?php echo $data->video_name;?>">
        <div class="overlaid"></div>
        <img src="<?php echo $image_url;?>"  width="93" height="89"/>
        </a>
        </div>
        <p title="Trailer Name"><strong><?php echo $data->video_name;?></strong></p>
        <p title="Released Date"><?php echo $data->video_released_date;?></p>
    </div>
<?php 
    if($i == 5){
        echo "<br clear = 'all'>";  
        $i=0;
    }
}
?>

您可以更改:

if($i == 5){
    echo "<br clear = 'all'>";    
}

if(!($i % 5)){
    echo "<br clear = 'all'>";    
}

试试这个:假设你的数组索引没有设置为奇怪的东西。

foreach ($videoEntries as $index=>$data) {
  if ($index % 5 == 0) {
    echo "<BR>";
  }
}
foreach($videoEntries as $data){
    $i++;
    ?>
<?php 
    if(($i % 5) == 0){
        echo "<br clear = 'all'>";  
    }
}
?>

只是为了完成示例。。。

只要需要循环的索引,就可以使用for循环(假设它是一个数组)。当您不需要索引时,foreach循环是为了方便而发明的。

for ($index = 0; $index < count(videoEntries); $index++)
{
  $data = $videoEntries[$index];
  ...
  if(($index % 5) == 0)
  {
    echo "<br clear = 'all'>";    
  }
}