Page 1 of 1

Reurn Query Results in Columns Instead of Rows

Posted: Wed Jun 07, 2006 9:10 am
by newphpguy
I am fairly new to PHP and wondering if there is an easy way to have my query results presented in columns instead of rows. The headings would be the first column, then each row of data from the query would be listed in its own column.

Thanks in advance for any thoughts on this.

Posted: Wed Jun 07, 2006 10:44 am
by newphpguy
Here is what I am doing as a solution. I write one of these for each item returned in the database. Is there a more effecient way to do this?

Code: Select all

<tr>
		<td class=bodytextbold>Info</td>
		<?
		$i=0;
		while ($i<$num){
			$info = mysql_result($results,$i,"info");
			echo "<td class=bodytext>" . $info . "</td>";
		$i++;
		}
		?>
	</tr>

Posted: Wed Jun 07, 2006 11:07 am
by GM
Have a look at the array_multisort function in the manual, it gives this example of how to turn rows into columns:

Code: Select all

// Obtain a list of columns
foreach ($data as $key => $row) {
   $volume[$key]  = $row['volume'];
   $edition[$key] = $row['edition'];
}
You'd need to modify this to something like:

Code: Select all

// Obtain a list of columns
foreach ($myResults as $key => $row) {
   $field1[$key]  = $row['field1'];
   $field2[$key] = $row['field2'];
   ...
   $fieldN[$key] = $row['fieldN'];
}
This gives you a set of arrays which you can manipulate to get the data out in columns.

Hope this helps.