Siddhu Siddhu - 2 months ago 8
jQuery Question

How to break foreach loop or for loop into 10 records

Suppose I have 56 records in my database. I'm using FOR LOOP to display them.
I want to append div after 10 records every time.

<?php
$a = 56;
for($i=0;$i<$a;$i++){
echo "<div>";
echo $i."</br>";
echo "</div>";
}
?>


output:


<div>0</div>
<div>1</div>
<div>2</div>
<div>3</div>
...
<div>56</div>



I want to append after 10 records each time

<div>
0
1
2
.
9
</div>
<div>
10
11
.
19
</div>
.
.
<div>
51
52
.
.
56
</div>

Answer

You can use the modulus operator for that.

<?php
$a = 56;
echo "<div>";
for($i=0;$i<$a;$i++){
    echo $i;

    if (($i+1) % 10 == 0) {
        echo "</div><div>";
    }
}
echo "</div>";
?>