Change while loop with foreach and count with !empty