0

I have a database with "7" user account objects.

I have called the objects using any of the loop functions.

foreach (objects as object) {
  echo '<div class="col-4>"'.object->property.'"</div>';
}

I want to add rows for each 3 columns as they are being displayed.

The above code would result in all my objects/colums/divs being nested in one row. I want a way in which I can have the loop adding a row for every 3 columns.

0

1 Answer 1

2

Count the objects as you go. If $x divided by 3 has no remainder, close and open the next div:

$x = 0;
foreach ($objects as $object){
    $x ++;
    echo '<div class="col-4">"'.$object->property.'"</div>';
    echo ($x % 3 == 0) ? '</div><div class="row">' : '';
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.