Reurn Query Results in Columns Instead of Rows

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
newphpguy
Forum Newbie
Posts: 6
Joined: Tue Jun 06, 2006 6:27 pm

Reurn Query Results in Columns Instead of Rows

Post 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.
newphpguy
Forum Newbie
Posts: 6
Joined: Tue Jun 06, 2006 6:27 pm

Post 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>
GM
Forum Contributor
Posts: 365
Joined: Wed Apr 26, 2006 4:19 am
Location: Italy

Post 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.
Post Reply