How to invert rows and columns in an HTML table populated by the return of a query?

Asked

Viewed 106 times

-1

I need to fill a table in my HTML page with data returned from a database query.

The problem is that the TABLE tag fills in line by line, from left to right.

In the layout I need to use, I need each row of the returned array to fill a column.

Detail, I need to use the TABLE tag because I need to print this in excel later.

    echo "<table>";
         while ($temp = $registros-> fetch_assoc()){
              echo "<tr>";
                   foreach ($temp as $mes){
                       echo "<th>".$mes."</th>";
                   }
              echo "</tr>";           
        }    
   echo "</table>";

Query return:
Array ( [Month] => January [col1] => 30 [col2] => 25)
Array ( [Month] => February [col1] => 30 [col2] => 25)
Array ( [Month] => March [col1] => 30 [col2] => 25)

Upshot: resultado do codigo acima

Desired: Como eu preciso que fique

2 answers

0

After days thinking about the best form, and based on the friend’s answer above, I came to the conclusion that the least laborious way is to create new arrays (one for each row of my table), and then fill the TABLE with the new arrays.

Simple but hard to think of when you’re in the middle of a full stack project.

0

This requirement would be the pivot in your query, one possibility is you change the query that exists and use UNION ALL, being a query for each column that you want to appear in your table, so you would not change your code in front-end

Browser other questions tagged

You are not signed in. Login or sign up in order to post.