my code causes the images to appear randomly within the page. But how to insert an \"element\" (in my case would be a div) between these images?
If you want to add a div(content) between those two pairs of images - add additional condition into your loop:
div
... for ($i=0; $i<4; $i++) { if ($i == 1) echo 'some content ...'; echo ''; }