Jump to content

Help Displaying Columns top down instead of left to right


extremeshannon

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;
}
else
{
	$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>';
			next($row);
			$y = $y + 1;
		}
		echo '</tr>';
		$i = $i + 1;
	}
	echo '</table></body></html>';
	mysql_free_result($result);
}

mysql_close ($link);
?>

Thanks for the help

 

Shannon

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.