use tables. Put each image in a table cell.
if your counter equals 0 then echo a <tr> first and increment.
if your counter equals 1 then echo a </tr> after and set to 0
Putting all the buy stuff should be just a matter of adding to your code. Post what you have and we can review it.