Skip to content
Advertisement

How can I output an elements inside a foreach by generating a column layout?

I’ve a problem I need to solve. I’ve this HTML layout here in PHP:

JavaScript

I have a column layout here. This means that I always want to pack 2 elements into a wrapper class. I get all my elements from an array, which I want to output with a loop. I had the idea to output the wrapper only every 2nd pass, but this results in the following output:

JavaScript

That’s my code:

JavaScript

My expected output in an image:

enter image description here

Advertisement

Answer

Your code will be a little complicated:

JavaScript

Less complicated solution is to split your array in chunks of size 2:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement