0

I have a while loop from database, but i want to wrap every 2 rows into a div.

Currently i have this but is only wrapping first row.

<?php
$get = $connect->query("SELECT * FROM sabiaque ORDER by ordem");
$num = 0;

while ($f = $get->fetch_array(MYSQLI_ASSOC)) {
    if ($num % 2) {
        echo '<div class="divisor-slide">';
    }
    echo' 
        <a href="'.$f['link'].'"><div class="item-a">   
            <div class="box1-item-a">
                <div class="i-wrap"><h1 class="i-white">'.utf8_decode($f['titulo']).'</h1><p>'.utf8_decode($f['subtitulo']).'</p></div>
            </div>
            <div class="box1-item-b">
                <img src="../'.$f['ficheiro'].'" style="max-width: 100%; height: 100%;" />
            </div>
        </div></a>
    ';
    if($num % 2) {
        echo '</div>';
    }
    $num++;
}
?>
2
  • if($num % 2) might be the culprit, try if($num % 2 === 0), reason being that modulus returns the remainder, php loosly evaluates that remainder to true (if it's non zero) Commented Aug 22, 2018 at 10:05
  • Another option is to create grouping of your array data using php.net/manual/en/function.array-chunk.php which then you can create a per row outer loop and a per colum inner loop, so your last div can be closed properly when you've got a single last item. Commented Aug 22, 2018 at 10:06

3 Answers 3

1

change this :

if($num%2) {
  echo '<div class="divisor-slide">';
 }

By this :

if( ($num % 2) == 0) {
  echo '<div class="divisor-slide">';
 }

And this :

if($num %2) {
  echo '</div>';
 }

To :

if( ($num % 2) == 0) {
  echo '</div>';
 }
Sign up to request clarification or add additional context in comments.

4 Comments

Consider to replace == with ===
It is still not working, it is wrapping every first row but the second row gets not inside the div
because your count start to 0 start your $num to 1
Yes i tought the same after replying but already tried it still the same, what changed is that it wraps every second row and the other is not wrapped inside the div
0

I often find I get into a muddle using these special $num%2 whatsits (I'll get all excited in being clever in using them, then forget later what they do when I revisit the code). Why not simplify it like this (although it uses a little more code):

$num = 1;
while ($f = $get->fetch_array(MYSQLI_ASSOC)) {
    if ( $num == 2 ) echo '<div class="divisor-slide">';
    // rest of code
    if ( $num == 2 ) echo '</div>';
    $num++; if ( $num == 3 ) $num = 1;
}

1 Comment

Thanks for this, it was the solution indeed, after a little thinking i figured out it was simpler then i tought, i could simply make a var with a set value of 1 then in the while loop make an if condition to check when the var was 1 to start the div and then after the content adding a var if the var is 2 then reset the counter to 1 again which is pretty much your solution
0

I find this working perfectly for me, in case someone might need it for the future.

<php?
$num = 1;
  foreach($results as $row) {
    if ( $num % 2 ){ 
      echo '<div class="divisor-slide">';
     }
    // your double div goes here....
    if ( $num % 2 == 0) {
       echo '</div>';
     }
     $num++;
  }

?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.