dostum while döngüsünün dışına en son tr tagından sonra ekle tablo koduna
if ($r % 4 == 0 ) 
{ 
echo "</tr><tr>"; 
$r=0; 
}