iainlang Posted April 5, 2007 Share Posted April 5, 2007 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. Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted April 5, 2007 Share Posted April 5, 2007 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>'; ?> Quote Link to comment Share on other sites More sharing options...
iainlang Posted April 7, 2007 Author Share Posted April 7, 2007 Thank you for your swift answer. I'll give it a go over the week-end. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.