Solved

PHP - display HTML table as inverted mysql table

Posted on 2010-08-25
3
307 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to avoid jquery conflicts in Wordpress ? 4 24
Split in Javascript 5 30
Centering Vertically 3 27
datetime in sql 6 24
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 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

20 Experts available now in Live!

Get 1:1 Help Now