Skip to content
Advertisement

How to turn a table vertical using PHP

I am just learning PHP ,and I want to create a table that display echo data that I submit to my database , the problem I have that the table displayed horizontally by default as you see Horizontal default table this my script

JavaScript

but I want it to be echoed vertically instead like this VERTICAL RESULT I WANT and I tried to change html in echo in my PHP code but I can’t get the result at all and the shape of the table is far away from what I want and this is the full script of my page .

Advertisement

Answer

Like everyone else said, you should convert your horizontal array into a vertical one.

Of course it should be a universal function to convert any query result, as opposed to hardcoding the row headings. The idea is to get each row’s array keys and use them as the keys for the new array and then add each corresponding value to the new array item.

Here is how it’s done in mysqli:

JavaScript

and then you get an array with desired structure which you can output using the code from ROOT’s answer:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement