7

I just want to know how to define HTML Tag <br clear="all"> after each 5 iteration in 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'>";    
    }
}
?>

Result Required or helps are definitely appricicated

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

5 Answers 5

5

Try this:

<?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;
    }
}
?>
Sign up to request clarification or add additional context in comments.

4 Comments

Thanx for sharing your experience with me
Every programer sould learn to do $i % 5, it is just too damn simple and usefull. Doing what is proposed here create a unnecesary need to mantain $i inside the 0 5 range. Thats asking for trouble.
@tei - so is ++$i==5, its not rocket science...the person maintaining the codebase should take care of that.
Too. Many. Moving. Parts. Anyway I don't want to stress my point. I am just adding my opinion here.
4

You can change:

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

to

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

1 Comment

thanx for sharing your experience with me
1

try this: Assuming your array index is not set to something strange.

foreach ($videoEntries as $index=>$data) {
  if ($index % 5 == 0) {
    echo "<BR>";
  }
}

3 Comments

That'll include it for all but the multiples of 5.
@MichaelRushton Yea, u caught me seconds before I posted my edit
@Churk Thanx for sharing your experience with me
0
foreach($videoEntries as $data){
    $i++;
    ?>

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

1 Comment

Thanx for sharing your experience with me
0

Just to complete the examples...

Whenever you need the index of the loop, you can use the for loop instead (assuming it's an array). The foreach loop was invented for convenience, when you don't need the index.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.