views:

569

answers:

8

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.

+4  A: 

Well, if you have multiple rows, with data formatted similarly in each row, I can't think of a way to create a table that avoids using a loop. That type of thing is basically what loops were invented for.

If you gave us more details on the table you want to build, maybe we can formulate a better method.

James Curran
+3  A: 

If you don't use some kind of loop then you need to hardcode the number of rows and columns in your code, which is probably a much worse idea

Gareth
+2  A: 

If the data in the table is dynamic then you have to use a loop. Even if you get a component that generates the table for you, internally it will use a loop.

Vincent Ramdhanie
+1  A: 

That depends on your definition of "best". FOR loops will do the job just fine. I'm not a PHP programmer, but in .NET, you can use the Repeater, a sort of template that you can use to declare the table HTML. It has templates for the header, the footer and each item. You bind a data source to the repeater, and it will generate the table HTML for you a bit more elegantly than using FOR loops. I imagine there might be some sort of templating equivalent in PHP.

In the end, though, even the Repeater uses some sort of loop...

+4  A: 

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.

Kent Fredric
I really like that. It makes for much neater code than loops with random strings in them.
Thomas Owens
that mangles HTML within PHP code, which is (IMNSHO) even worse than mangling code within HTML.
Javier
PHP is mangled as it is - you can't make it much worse, IMO.
Thomas Owens
Remember to escape data with htmlspecialchars, when embedding it in HTML
troelskn
And your functional style example at the end won't work - PHP doesn't have lexical scope.
troelskn
Doing this will make it much worse! ;-)
Colonel Sponsz
Bugger. It'll need an object or *puke* globals. 5.3 will have closures!.. but... a crappy namespace system.
Kent Fredric
"Those who don't know Lisp are condemned to reinvent it, poorly". Have you ever seen a Lisp 'web library'? they throw away all the elegance of the language
Javier
+2  A: 

I highly recommend the Smarty templating engine for PHP. This will permit you to move your table (and the associated loop, which is unavoidable) into the template using Smarty syntax, so it will be clearer and more separated from your business logic. You would be able to replace nearly all the code in your example with a few simple tags in an HTML snippet. Check out the crash course on the Smarty page for an example of using a table.

rmeador
A: 

I usually use loops and implode for this, in PHP. In a language, which supports functional programming I would do it differently.

function render_table($data) {
  $html = '<table>';
  $tr = array();
  foreach (array_keys($data[0]) as $key) {
    $tr[] = '<th>' . htmlspecialchars($key) . '</th>';
  }
  $html .= "\n" . '<thead>' . "\n" . '<tr>' . "\n" . implode("\n", $tr) . '</tr>' . '</thead>';
  $tbody = array();
  foreach ($data as $row) {
    $tr = array();
    foreach ($row as $value) {
      $tr[] = '<td>' . htmlspecialchars($value) . '</td>';
    }
    $tbody[] = '<tr>' . "\n" . implode("\n", $tr) . '</tr>';
  }
  $html .= "\n" . '<tbody>' . "\n" . implode("\n", $tbody) . '</tbody>';
  $html .= '</table>';
  return $html;
}
troelskn
A: 

Why do you want to avoid loops? If you have a set of items, and you want to display each item, surely looping over the set is the obvious solution?

If you are trying to separate the model from the view, or write more readable code, good; but loops are not inherently bad.

Hugh Bothwell
No, they aren't bad. But when you have a massive data set and are doing weird things with it, loops don't help in making code readable.
Thomas Owens