2

Please I want to display some informations between a queried database while loop results:

<?php

    $query = mysqli_query($con, "SELECT * FROM table");
    while($sql = mysqli_fetch_array($query)){
        $id = $sql['id'];
        $country = $sql['country'];

        echo $id.". ".$country."<br>";

    }

?>

If the result is:

1. MALAYSIA
2. GERMANY
3. EGYPT
4. CAMEROUN
5. ITALY
6. RUSSIA
7. ENGLAND
8. ETHIOPIA
9. AUSTRIA
10. JAPAN

I want to add something in between the result like this:

1. MALAYSIA
2. GERMANY
3. EGYPT
this is an advert div(1)
4. CAMEROUN
5. ITALY
this is an advert div(2)
6. RUSSIA
7. ENGLAND
8. ETHIOPIA
9. AUSTRIA
this is an advert div(3)
10. JAPAN

Please is it possible?

4
  • Yes. It is possible. A simple count would work. But what is the logic of interval here? 3, 2, 4?? Commented Mar 28, 2016 at 7:46
  • please how can it be done? Commented Mar 28, 2016 at 7:48
  • What pattern or logic you are following to print advert divs? Commented Mar 28, 2016 at 7:51
  • i also store the adverts in a database table also so I can query just one at each div in a random order Commented Mar 28, 2016 at 7:55

2 Answers 2

1

It will insert ads in every third row.

$count    = 0;
$ad_count = 1;

$query = mysqli_query($con, "SELECT * FROM table");
while($sql = mysqli_fetch_array($query)){
    $id = $sql['id'];
    $country = $sql['country'];

    echo $id.'. '.$country.'<br>';
    $count++;

    if ($count%3==0)
    {
    echo 'this is an advert div('.$ad_count.')<br>';
    $ad_count++;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

<?php

$count = 1;
$advertCount = 1;
$advertArr = [3, 5, 9];
$query = mysqli_query($con, "SELECT * FROM table");
while($sql = mysqli_fetch_array($query)){
    $id = $sql['id'];
    $country = $sql['country'];

    echo $id.". ".$country."<br>";

    if (in_array($count, $advertArr)) {
        echo "this is an advert div($advertCount++)";
    }

    $count++;
}

?>

Though I have not tested the above solution, but I think this will help you.

Comments

Your Answer

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