Horizontal And Vertical Presentation Of Db Table

How can I retrieve a row from a database and print it out both horizontally and vertically?

I have a table which contains data in the following columns:

The data in the table is:

When I retrieve these values from the database I want to print out like this:

Any ideas of how to achieve this in a foreach loop?