0

I have list of records to display in one div and another div. Showing the first 10 records in one div and other following records in another div. i tried to get it but not showing the results

$results output:

Array ( [id] => 124 [number] => 330 [datetime] => 2021-12-12 15:30:00 ) 
Array ( [id] => 123 [number] => 123 [datetime] => 2021-12-12 12:30:00 ) 
Array ( [id] => 122 [number] => 143 [datetime] => 2021-12-12 12:00:00 )

Code

foreach ($results as $result) {
  if condition to check first 10 rows to show 
     echo <div>first 10 records</div>
  else{
        echo <div>Following after 10 records to show</div>
      }
  }
mike
  • 31
  • 4
  • In foreach add a $count variable that starts at 0 and $count++ each loop. Than check it and if its more than 10 write to another div – Undry Dec 12 '21 at 15:00

1 Answers1

0

The modulus operator can be used for this. Rough example:

<?php
$range = range(1,30);
?>
<div>
    <?php foreach($range as $i => $r) {
        if(!empty($i) && $i % 10 == 0) {
            echo '</div><div>';
        }
        echo $r . PHP_EOL;
    }
    ?>
</div>

Demo: https://3v4l.org/1JfZV

user3783243
  • 5,368
  • 5
  • 22
  • 41
  • The use of `empty()` in this context is not ideal because `$i` will always be declared. It could be more simply written as `if ($i && $i % 10 == 0) {`. – mickmackusa Dec 12 '21 at 14:25