Perché qui
echo '<th colspan="1">' . $nomecol[$j] . '</th>';

metti il colspan?