mysql - php foreach display limit and after it display the rest until the limit again
I want to show 16 data from database per page and foreach 8 of them display
<div>There are 8 Data </div>
after showing the rest 8 to display again
<div>There are 16 Data </div>
something like that if possible of course
I tried to use the break after showing 8 data and trying to continue again but without success
the code I tried:
$sn_count = 1;
$html = '';
$display_datadiv_every = 8;
foreach($result as $point){
$html .= "<div class=\"name\">".$sn_count."</div>"
. "<div class=\"pointsurname\">"
. $point['name']
. "</div>";
if($sn_count++ % $display_datadiv_every == 0)
{
echo '<div class="card d-flex mt-2 mb-4" style="width: auto;
flex-direction: row;
height: 110px;">
<div class="card-body">
<h5 class="card-title">There are".$sn_count." data</h5>
</div>
</div>';
}
} echo $html;
}}
and it displays me like this:
<h5 class="card-title">There are".$sn_count." data</h5>
<h5 class="card-title">There are".$sn_count." data</h5>
foreach 8 data displayed one div and
$point['name'] 16 times
$point['name']
Answer
Solution:
First define a counter variable outside the loop. Then put a condition inside the loop that it should show the first 8 result in the first8 div and show rest result in else condition of second div. Then counter++ inside loop
<?php
$counter = 0;
foreach($result as $point){
if($counter < 8){
?>
<div class="first8"><?php echo $point; ?></div>
<?php } else { ?>
<div class="second8"><?php echo $point; ?></div>
<?php } ?>
<?php $counter++;
}
?>
Source