Jump to content

Help Displaying Columns top down instead of left to right


Recommended Posts

Hello everyone,

I am trying to display my Columns from the top down. I want my Column names on the left and the data moving to the right.


Column Record1 Redcord2 Record3

C1          Data         Data       Data

C2          Data         Data       Data

C3          Data         Data       Data

C4          Data         Data       Data

C5          Data         Data       Data


This what I am using and it is traditional left to right output with each record below the next.

if (!$result) 
	$message = 'ERROR:' . mysql_error();
	return $message;
	$i = 0;
	echo '<html><body><table><tr>';
	while ($i < mysql_num_fields($result))
		$meta = mysql_fetch_field($result, $i);
		echo '<td>' . $meta->name . '</td>';
		$i = $i + 1;
	echo '</tr>';
	$i = 0;
	while ($row = mysql_fetch_row($result)) 
		echo '<tr>';
		$count = count($row);
		$y = 0;
		while ($y < $count)
			$c_row = current($row);
			echo '<td>' . $c_row . '</td>';
			$y = $y + 1;
		echo '</tr>';
		$i = $i + 1;
	echo '</table></body></html>';

mysql_close ($link);

Thanks for the help



Link to comment
Share on other sites

you would 'pivot' the data, i.e. swap the rows and columns - 
// retrieve the data, swapping rows/columns
$pivot = array();
while($row = a_fetch_assoc_statement()){
    foreach($row as $key=>$value){
        $pivot[$key][] = $value;

// display the data
echo "<table>";
foreach($pivot as $key=>$row){
    echo "<tr><th>$key</th><td>"; // the row heading
    echo implode('</td><td>',$row); // the data in the row
    echo "</td></tr>\n";
echo "</table>";

the a_fetch_assoc_statement() in the above would be replaced with the appropriate statement from the database library of functions you are using. however, the mysql_ functions are obsolete and you should be using either PDO or msyqli_ statements.

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.