Ultimately this is more of a css and html issue than any php issue. I see no apparent difference between your code and using divs amd p tags (with maybe a header tag thrown in there as weel). Either way, css can do what you want.
first thing you'd need to do is create a containing div which wraps your table output (so open a div before the opening php tag and close it after the php tag). Give that div an id so you can more easily reference it in css. Then in css make the div have a width of 800px. Then just float all tables that are children of that div left. When the table is shown, there won't be room left for a fifth so it will appear on the next row.
That same theory could be applied using divs and p tags as weel. The table becomes a div, your first row an h3 and the other rows become p's.
Otherwise you could look at the value of $numberofproducts andlook at the remainder when divided by 4. If it's 0, echo a br tag. Something like:
if($numberofproducts % 4 === 0)
{
echo '<br />';
}
That would go directly after the table echo statement.
Hope that helps.
Sorry for any spelling mistakes, I'm on my phone.
BTW... that &34; thing should be a percent sign (the modulus operator). And there should be the actual html code for a <br /> tag.... not just a new line. Thank you horrible phone for that.