while list 4 in 4 result within div

Asked

Viewed 399 times

5

How do I list the result of an sql query so that 4-in-4 line is inside a div.

Ex. expected result:

<div class="row">
    <span>1</span>
    <span>2</span>
    <span>3</span>
    <span>4</span>
</div>

<div class="row">
    <span>5</span>
    <span>6</span>
    <span>7</span>
    <span>8</span>
</div>

<div class="row">
    <span>9</span>
    <span>10</span>
</div>

Testing:

$count = 0;
while ($count <= 10) {
    if ($count % 4) {
        echo '<div class="row"><span>'.$count.'</span></div>';
    }

    $count++;
}

3 answers

7


This version has logic similar to the author’s attempt and @Guilherme’s response, using the %. The difference is that the module calculation was done in 2 separate lines, to avoid the repetition of echos of div:

$count = 10;
$group = 4;

for ( $i = 1; $i <= $count; ++$i ) {
    if ( ( $i - 1 ) % $group == 0 ) echo '<div class="row">';
    echo '<span>'.$i.'</span>';
    if ( $i == $count || $i % $group == 0 ) echo '</div>';
}

See working on IDEONE.


Follow an alternative with two loops, which can be adapted to more complex scenarios:

    $count = 10;
    $group = 4;

    for ( $i = 1; $i <= $count; $i += $group ) {
        echo '<div class="row">';
        for ( $j = 0; $j < $group && $j + $i <= $count ; $j++ ) {
            echo '<span>'.($j + $i).'</span>';
        }
        echo '</div>';
    }

See working on IDEONE.

  • As an experiment, I made a version of a single line. In practice, this should not be done in order not to be unreadable at the time of maintenance. http://ideone.com/xlbAAc

5


What’s left is to isolate the Ivds .row of the elements <span> or if span appears independent of % 4 then he shouldn’t even be inside the while and the % 4 should always be compared to 1 or 0 (as a suggestion from @Bacco to simplify we will start comparing with the % 4 zero):

<?php
$count = 0;

echo '<div class="row">', PHP_EOL;

while ($count <= 10) {
    if ($count > 0 && $count % 4 === 0) {//A cada quatro deve fechar o .row e abrir novamente
        echo '</div>', PHP_EOL, PHP_EOL, '<div class="row">', PHP_EOL;
    }

    echo '<span>', $count, '</span>', PHP_EOL;

    $count++;
}

echo '</div>';

One important thing is that your code is not generating 10 items, but 11, because it starts at zero, so if the increment starts from 1 the code would be simpler, because the amount would be 10, it will be necessary to check if it is greater than 1:

<?php
$count = 1;

echo '<div class="row">', PHP_EOL;

while ($count <= 10) {
    if ($count > 1 && $count % 4 === 1) {//A cada quatro deve fechar o .row e abrir novamente
        echo '</div>', PHP_EOL, PHP_EOL, '<div class="row">', PHP_EOL;
    }

    echo '<span>', $count, '</span>', PHP_EOL;

    $count++;
}

echo '</div>';
  • 1

    Cara turned very good, vlw @Guilhermenascimento

0

I did it that way:

    $count = 0;
    while ($count <= 10) {
        if ($count % 4) { 
            echo '<span>' . $count . '</span>';
        } else {
            if($count === 0 ){
            echo '<div class="row">';
            echo '<span>' . $count . '</span>';
            } else {
                 echo '</div>';
                 echo '<div class="row">';
                 echo '<span>' . $count . '</span>';
            }

        }
        if($count === 10){
            echo '</div>';
        }

        $count++;
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.