use COUNT and insert X DIV in X quantity

Asked

Viewed 133 times

1

Assuming I have a database with X elements! At the time of the query I will use a count to see how many lines I have! I would like to divide this result and divide by any number, in which case I will use the value of 3,taking into account that I had a return of 9 lines, the result will be 3!

So at the time of echo I’d like to insert one for div in "turn" of each 3 results. I would like the result to look like the following!

<div class="inserida_pela_php">
    <div class="div_qualquer"> 1 </div>
    <div class="div_qualquer"> 2 </div>
    <div class="div_qualquer"> 3 </div>
</div>

<div class="inserida_pela_php">
    <div class="div_qualquer"> 4 </div>
    <div class="div_qualquer"> 5 </div>
    <div class="div_qualquer"> 6 </div>
</div>

<div class="inserida_pela_php">
    <div class="div_qualquer"> 7 </div>
    <div class="div_qualquer"> 8 </div>
    <div class="div_qualquer"> 9 </div>
</div>

1 answer

2


<div class="inserida_pela_php">
<?php
$count = 9;
for ($i = 1; $i <= $count; $i++) {
    echo '<div class="div_qualquer">' . $i . '</div>' . "\n";
    if ($i % 3 == 0 && $i != $count) {
        echo '</div><div class="inserida_pela_php">' . "\n";
    }
}
?>
</div>

Fit to your needs

  • Great! I’ll even study more with this piece of yours!

  • Unfortunately I don’t remember what it’s called, but when you use % it splits and returns the rest, like 9%8=1

  • Only complementing: % Means Module , is one of the arithmetic operators! it returns the rest of a division, $a % $b the result will be the rest of $a divided by $b.

  • I need to add a query to the database!! And distribute the data between the columns! I used! But instead of distributing the 18 results by the div! It ta repeating everything every time the div "inserted_pela_php"

  • Put somewhere the code you have so far and maybe I can help.

Browser other questions tagged

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