I want to loop extra deetails.. by calculate the total items divided five. For example, i have 2 items, then i will add extra 3 div to it. When i have 8 items, i will add extra 2 div to it.
$totalcount = 2;
$check = $totalcount %5;
$totalcount = 8;
$check = $totalcount %5;
I found out i cant use this method because it loops wrongly. i will use extra loop to show the result when detect the main loop when reached end of loop.
if($key == $len - 1)
for ($i = 0; $i < $check; $i++) {
// do something
}
}
Is there any solution, i can ensure $totalcount%5
is always equal to zero and add extra div details to it?
You have to do little bit of calculation to find out extra number of divs required. Given the fact that $totalcount
is your number of rows, $totalcount
would be your necessary number of divs. And subsequently you would have to calculate extra number of divs in the following way,
$totalcount = 7; // or some other value
$totaldivs = $totalcount + (5 - ($totalcount % 5));
$extradivs = $totaldivs - $totalcount; // extra divs
Finally you can use two loops, one printing actaul rows inside divs and other printing extra divs, like this:
// loop printing rows inside divs
for($i = 0; $ < $totalcount; ++$i){
echo '<div>';
// your rows
echo '</div>';
}
// loop printing extra divs
for($i = 0; $ < $extradivs; ++$i){
echo '<div>';
...
echo '</div>';
}
Sidenote: I kept if($key == $len - 1){ ... }
block outside of the equation but you can place it in the appropriate place as per your requirement.