[thelist] another PHP Sql question...

Kelly Hallman khallman at wrack.org
Fri Aug 29 11:27:58 CDT 2003

A good way to think of a problem like this is to break down how you would 
do it manually. Play computer! Visually you know what you want. Your brain 
knows how to do this without too much thought.. you write a then b, then 
start a new row then write c then d, and so on.. pretty easy to code, too! 
You've just got to tell the computer the rules...

$numcol = 2;
$oncol = 0;

while($row = $result->fetchRow(DB_FETCHMODE_ASSOC)) {
    if ($oncol < 1) { print "<tr>"; }
    print "<td>{$row}</td>";
    if ($oncol >= $numcol) { print "</tr>\n"; $oncol = 0; } }

The only problem with that is if your last row is not in the last column, 
the table row will not be closed. So we'll add just a bit more code:

if ($oncol < $numcol) {
    print str_repeat("<td>&nbsp;</td>", ($numcol - $oncol)). "</tr>\n"; }

That will add empty cells for the remaining table columns in that row and 
close the row.  Surround all of this with <table> </table> and you've got 
a nice table with the results as you want them. Also, if you want to make 
it three or more columns later, you only have to change one variable.

If you examine that code, it's probably pretty close to the thought 
process you'd be going through if you were to write this output by hand.


On Fri, 29 Aug 2003, jsWalter wrote:

> I am querying a database just fine.
> getting back what I expect, just fine.
> But now I've been thrown a wrench in the form of double row display.
> my client wants...
>      a    b
>      c    d
>      e    f

Kelly Hallman

More information about the thelist mailing list