Responsive loop with 3 columns inside row then 2 columns

You can control this with media queries in CSS.

@media screen and (max-width: 768px) { div.row { width: 50%; } } /* for iPad */

@media screen and (max-width: 600px) { div.row { width: 100% } } /* for mobile phones */

I hope this helps.

UPDATE:
You can’t do this with PHP because PHP is executed on the server before the content is delivered to the browser, so there is no time for PHP to determine which device or viewport is in use. Have a look here.