To do it all in one loop you need build your html in strings rather than output it. Store each section in a different variable and then echo these strings after your loop. There isn’t any way to output two different section of html in a single loop other than this.
The only other choice you have is to construct multiple loops and loop over the rows multiple times.