PHP/MySQL Output In Two Columns, Newspaper Style

OK – here’s another dilemma that I searched the web high and low for, and couldn’t find a solution, so I made it myself.

My dilemma was that I wanted to query a MySQL table and write the first half of the output in one column, and the second half in an adjacent column. For example, say my table has 65 records in it – I want to print records 1-33 in the first column, and 34-65 in the second column. Everything I found had the output with all odd numbered records in the first column and all the even numbered records in the second column. Like:

1 2
3 4
5 6

I wanted:

1 4
2 5
3 6

Here’s how I wrote the code:

echo(“<table cols=3 border=0>\n”); // create main container table
echo(“<tr><td valign=’top’>\n”);
$sql=mysql_query(“SELECT blah1,blah2 FROM blah3 ORDER BY blah1″);
if ( $leftover == 1 )
$half+= 0.5;
echo(” <table cols=1 border=0>\n”); // create table within the outer table left-hand cell
while ($result=mysql_fetch_array($sql)) {
if ( $i >= $half ) {
echo(“</table>\n”); // close the left-hand cell table
echo(“</td><td width=25> </td><td valign=’top’>\n”); // close the outer left-hand cell, put in a filler cell for space, and open the outer right-hand cell
echo(“<table cols=1 border=0>\n”); // create table within the outer table right-hand cell
$half=999999999; // make sure half will now always be greater than $i
echo(” <tr><td>$blah1</td></tr>\n”);
echo(” </table>\n”); // close table within the outer table righ-hand cell
echo(” </table>\n”); // close outer container table

Note that if you copy/paste this code, WordPress likes to make “smart quotes” with the left hand quote symbol being different from the right hand quote symbol. Make sure all your double quotes are really double quotes and single quotes are really single quotes.