Thomas Owens Thomas Owens - 2 months ago 16
HTML Question

Are loops the best way to build a table?

I have to build an HTML table that shows data for users versus pages visited. It seems klunky to use for and/or foreach loops, but I can't think of anything better. I'm using PHP, but I would assume that this is language agnostic.

Answer

Avoiding loops is probably not possible, if not in implementation, then it will still happen at machine level.

However, if you want to try stay 'pure' without nasty code, you can at least do:

$tableformat = '<table><thead>%s</thead><tbody>%s</tbody></table>';
$rowformat   = '<tr>%s</tr>'; 
$cellformat  = '<td>%s</td>'; 

$hdata = '';
foreach( $data[0] as $cellname => $cellvalue )
{
   $hdata  .= sprintf( $cellformat, $cellname ); 
}
$hdata = sprintf( $rowformat, $hdata ); 
$rdata = "";
foreach( $data as $rownum => $rowvalue ) 
{ 
   $row = "";
   foreach( $rowvalue as $colname => $colvalue ) 
   {
       $row .= sprintf( $cellformat, $colvalue );  
   }
   $rdata .= sprintf($rowformat,$row); 
}
return sprintf( $tableformat, $hdata, $rdata );

At least that way it might be somewhat maintainable. and you don't have much worry about incomplete strings.

You could also subsitutue some of that code with

   $hdata = "<tr><td>" . implode( "</td><td>", array_keys( $data[0] )) . "</td></tr>";
   $rdata .= "<tr><td>" . implode( "</td><td>", $rowvalue ) . "</td></tr>";

Which while being rather concise, will possibly get you in a bit of hot water eventually and it will need recoding. ( and implode internally runs a loop, so its a lose-lose ).

If you don't mind the over head of extra function calls ( and still running loops ) this could make for more concise code without too many negatives:

 $tableformat = '<table><thead>%s</thead><tbody>%s</tbody></table>';
 $rowformat   = '<tr>%s</tr>'; 
 $cellformat  = '<td>%s</td>'; 

function tr( $cells )
{  
   $o = "";
   foreach( $cells as $i => $v )
   {  
      $o .= sprintf( $cellformat, $v ); 
   }
   return sprintf( $rowformat, $o );  
}


return sprintf( $tableformat, 
               tr( array_keys($data[0])), 
               implode("", array_map( 'tr', $data )) );

But beware, thats hard to read, and you keep that up one day you'll wake up with lisp.

Comments