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.
Reurn Query Results in Columns Instead of Rows
Moderator: General Moderators
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>Have a look at the array_multisort function in the manual, it gives this example of how to turn rows into columns:
You'd need to modify this to something like:
This gives you a set of arrays which you can manipulate to get the data out in columns.
Hope this helps.
Code: Select all
// Obtain a list of columns
foreach ($data as $key => $row) {
$volume[$key] = $row['volume'];
$edition[$key] = $row['edition'];
}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'];
}Hope this helps.