Ademaintain
Ademaintain

Reputation: 137

How to Echo an Item after three loops in php

I have a scenero while i need to print an advert after every three items loop in php. I am looping like 100 video from database and after every three number of video count loop, it should echo an advert and continue, that it, echo an image in between every three items of video

like

<div>1<div>
<div>2<div>
<div>3<div>
ADVERT HERE
<div>4<div>
<div>5<div>
<div>6<div>
ADVERT HERE  ......... and so on

Below is what i have tried

<?php 
$qsel = "SELECT * FROM video_post ORDER BY sn DESC";

$results = mysqli_query($conn,$qsel) or die(mysqli_error());  
$num_rows = mysqli_num_rows($results);
$i = 0;
while($rows=mysqli_fetch_array($results)){
    $i = $i++;
    $status = $rows['status'];
    $usern = $rows['username'];  ?>


        ///the advert
        <?php
        if($i == 3){
            print '<div class="wrapper">
                <div class="image">
                <img  src="advert/advert 4.jpg" alt="Slide 1" style="width: 65%;">
                </div>
                </div>';
            // $i=1;
        }   ?>


    <video width="320" height="240" class="embed-responsive-item" controls>
        <source src="video/<?php echo $video ; ?>" type="video/mp4">
        Your browser does not support the video tag. 
        </video>



        <?php } ?>

Upvotes: 1

Views: 359

Answers (1)

TrippyD
TrippyD

Reputation: 735

Modulo (%) is your friend https://secure.php.net/manual/en/language.operators.arithmetic.php

change

 if($i == 3){

to

 if( ($i % 3) == 0 ){

Upvotes: 2

Related Questions