How to iterate through a collection of items and display them organised in a matrix with modulo operator


lets say you want to display a collection of items organised in rows and columns, and you do not want to use tables, and rather want to go with a tableless design.

This is the result you are aiming at:

item 1
item 2
item 3
</div><!-- end row -->
item 4
item 5
item 6
</div><!-- end row -->
item 7
item 8
item 9
</div><!-- end row -->

A good way to do it would be to use the modulo operator. In PHP, you can put a counter and use the counter to change line using the modulo operator. If you want to have 3 items in the row, do $n%3, when $n%3 == 1 that means you should start a row, when $n%3 == 0 it means you should close the row…

View original post 18 more words