Fixed parse errors and undefined variable in foreach loop