You should be doing this in PHP as html. A CSS Grid container with Flex-Direction:row & grid-template-columns:repeat(3, 1fr) & grid-template-rows:auto will automatically take your SQL data that you format within it and arrange them 3 across breaking to a new line after each group of 3.