So I am able to display 3 items per row in a webpage. However, the problem arises if there is an extra item. Say I have an array with items 1,2,3,4,5,6,7,8,9,10,11
. I am able to display items 1 to 9. But unable to display 10
and 11
due to an index error, if I add 12
, it will all be fine.
What I want is to display from this.
<div class='row-0'>
<p>1</p>
<p>2</p>
<p>3</p>
</div>
<div class='row-1'>
<p>4</p>
<p>5</p>
<p>6</p>
</div>
<div class='row-2'>
<p>7</p>
<p>8</p>
<p>9</p>
</div>
To this:
<div class='row-0'>
<p>1</p>
<p>2</p>
<p>3</p>
</div>
<div class='row-1'>
<p>4</p>
<p>5</p>
<p>6</p>
</div>
<div class='row-2'>
<p>7</p>
<p>8</p>
<p>9</p>
</div>
<div class='row-3'>
<p>10</p>
<p>11</p>
</div>
Here is my demo code so far:
<?php
$arr = array(1,2,3,4,5,6,7,8,9,10);
$row=0;
$i=0;
while(true){
echo "<div class='row-".$row."'>\n";
while(true){
echo "<p>".$arr[$i]."</p>\n";
$i++;
if($i%3===0){
break;
}
}
echo "</div>\n";
$row++;
if($arr[$i]==count($arr)){
break;
}
}
?>
Any tips? Thank you for reading.