Jump to content

Two-column display ordered alphabetically


iainlang

Recommended Posts

I'm trying to list meteorological data reports, from an ever-varying number of locations, in two columns, side-by-side - the first half of the reports listed alphabetically in the left column and then the second half of the reports listed in the right column so that the list runs alphabetically down the first column then goes to the top of the second column and continues down from there. In the example below, numbering the locations for the purpose of the example, it has to be something like this, using, say, 60 locations -

 

<table>

<tr><td>First column</td><td>Second column</td></tr>

$Query="SELECT * FROM observations order by location";

$Result=mysql_db_query ($DBName, $Query, $Link);

while ($Row=mysql_fetch_array ($Result))

(

 

<tr>

<td>$Row[location1] $Row[humidity1] $Row[oktas1]</td><td>$Row[location31] $Row[humidity31] $Row[oktas31]</td>

</tr>

 

<tr>

<td>$Row[location2] $Row[humidity2] $Row[oktas2]</td><td>$Row[location32] $Row[humidity32] $Row[oktas32]</td>

</tr>

 

}

 

</table>

 

and so on until the database is exhausted.

I've tried using arrays to store each every second data item and then every other data item but I think it's clumsy.  (It *IS* clumsy!)  Is there a more elegant way to do it?

TIA.

This should work...although I haven't tested it.

 

<?php
$Query = "SELECT * FROM observations order by location";
$Result = mysql_db_query($DBName, $Query, $Link);

//put the data in an array we can access out of order (not linearly)
while ($Row = mysql_fetch_array ($Result)) {
$data[] = $Row;
}

//start the table
echo '
<table>
	<tr>
		<td>First column</td>
		<td>Second column</td>
	</tr>';

//loop through...we only want the first half directly...we'll get the second
//half through addition.
for ($i = 0; $i < floor(count($data) / 2)); $i++) {
$col_2 = $i + floor(count($data) / 2);
echo '
	<tr>
		<td>' . $data[$i]['location'] . " " . $data[$i]['humidity'] . " " . $data[$i]['oktas'] . '</td>
		<td>' . $data[$col_2]['location'] . " " . $data[$col_2]['humidity'] . " " . $data[$col_2]['oktas'] . '</td>
	</tr>';
}

//check to see if there is an odd number of rows...if there is, add on the odd one
if (count($data) % 2 == 1) {
echo '
	<tr>
		<td> </td>
		<td>' . $data[count($data) - 1]['location'] . " " . $data[count($data) - 1]['humidity'] . " " . $data[count($data) - 1]['oktas'] . '</td>
	</tr>';
}

//close the table
echo '
</table>';
?>

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.