Solved

PHP - display HTML table as inverted mysql table

Posted on 2010-08-25
3
305 Views
Last Modified: 2013-12-13
Hi all,

I have a mysql table that has 10 columns and 2 rows.
I can easly create a PHP script which will create an HTML table and display the data as is.

What I want to do is invert the rows and columns when displayed in HTML - ie...the 2 row titles from mysql become the column headings and the 10 columns from mysql become the first cells in each row in the the HTML table....obviously the data in the other cells will still be displayed in the right cells.

Can anyone show me a code example for how to achieve this?
0
Comment
Question by:amrox
3 Comments
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 33526447
0
 
LVL 10

Accepted Solution

by:
racmail2001 earned 500 total points
ID: 33526644
Try the following code

hope it helps
<?php



	$db=mysql_connect('localhost',"root","") or die('Could not connect !');

    mysql_select_db("TEST",$db) or die('Could not find the database');



$query="select * from TABLE where 1";

$result = mysql_query($query);

$i=0;

while($row=mysql_fetch_row($result)) $array[$i++]=$row;

echo "<table border=1>";

for ($x=0;$x<count($array[0]);$x++){

	echo"<tr>";

	for ($y=0;$y<count($array);$y++){

		echo"<td>".$array[$y][$x]."</td>";

	}

	echo"</tr>";

}

echo"</table>";



?>

Open in new window

0
 
LVL 4

Author Closing Comment

by:amrox
ID: 33529382
fantastic, exactly what I needed, worked out of the box (so to speak), thanks very much
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
PHP loop not working 4 33
Modify Table Width 6 14
Bootstap Icons 3 22
selecting date modified field from a table 2 0
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now