PHP: Group data without using group_by

Asked

Viewed 84 times

0

Does anyone know how to group data without using group_by in consultation?

I need every 10 loop results (foreach), create a div, that is, 10 records within each div, I don’t care about the values.

<?php foreach($resultados->result() as $resultado){ ?>
    <?php for($i = 0; $i <= 10; ++$i){ ?>
        <div>
    <?php } ?>
        <?=$resultado->id?>
    <?php for($i = 0; $i <= 10; ++$i){ ?>
        </div>
    <?php } ?>
<?php }// foreach ?>
  • I made a quick example: https://hastebin.com/awokiviyon.xml

  • IT WORKED! Thanks for the help, the last <div> generated is empty, but it’s working for me. How do I give a note for the help?

  • @Andersonfelipe To give a point, is the "triangle" up. To accept the answer, check the "V" which will turn green. IMAGE

1 answer

0

One way to do it would be by using an auxiliary counter ($c) and module (%).

Example:

$c = 0;
foreach($resultados->result() as $resultado){

    if ($c % 10 == 0) echo  '<div>';
    echo $resultado->id;
    if ($c % 10 == 0) echo  '</div>';

    $c++;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.