Page 1 of 1

PHP and CSS Compatible Table

Posted: Tue Jul 07, 2009 11:44 pm
by lolard
I know its fairly straight-forward to create a simple HTML table with data filled-in using PHP and a database, but how do you create a HTML table so that its style can be modified using CSS and still being able to be filled-in with PHP?

Any help and/or code would be greatly appreciated. Thanks.

Re: PHP and CSS Compatible Table

Posted: Wed Jul 08, 2009 12:06 pm
by kaszu
Easy:

Code: Select all

<style type="text/css">
    table { border: 1px solid black; }
    table td { background: #fff; }
    table .odd td { background: #f9f9f9; }
</style>
 
<table>
<?php foreach($data as $k => $row) { ?>
  <tr class="<?php echo ($k % 2 ? 'odd' : 'even'); ?>">
      <td><?php echo $row['a']; ?></td>
      <td><?php echo $row['b']; ?></td>
  </tr>
<?php } ?>
</table>
Please be more specific where you are having problems.