How to define an html tag after every 5th iteration in a foreach loop
I just want to know how to define the HTML tag <br clear="all"> after every 5 iterations in the foreach loop, here is my code
<?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'>"; } } ?> Need result or help definitely appricicated
12345 <br clear="all"> 678910 <br clear="all"> Try the following:
<?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; } } ?> You can change:
if($i == 5){ echo "<br clear = 'all'>"; } to
if(!($i % 5)){ echo "<br clear = 'all'>"; } try the following: Suppose your array index is not set to something strange.
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'>"; } } ?> Just to complete the examples ...
Whenever you need a loop index, you can use a for loop instead (assuming its array). The foreach was invented for convenience when you don't need an index.
for ($index = 0; $index < count(videoEntries); $index++) { $data = $videoEntries[$index]; ... if(($index % 5) == 0) { echo "<br clear = 'all'>"; } }