PHP – Build a HTML Table From any Query

As far as I am putting some efforts towards PHP and understanding why it is so hated by programmers all around, I have decided that the results from the queries should be always easily displayed in an HTML table. A beautiful one, if possible 🙂

Until now, what I have used to do was to tell php how to build the table, explicitly mentioning every column name. Today I have researched a little and I found out that this is really not needed – the PHP provides two beautiful options towards fetch_fields and fetch_arrays to understand the name of the columns in the table and the number of columns. Thus, without specifying them, with a few loops we are able to build table like this:

table

Just from the query. 🙂 And yes “Beauty” in programming is a formidable notion. 🙂

Ok, how have I done it? Pretty much through the help of two embedded loops, and the aforementioned fetch_fields and fetch_arrays.

Check the code yourself:

Also available in GitHub! 🙂

Enjoy your day and have fun 😀

Tagged with: , , ,