Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a decent HTML-table-generator class in PHP? [closed]

I'm looking for a decent class in PHP which will generate complex HTML tables, i.e. it should support colspan/rowspan and individual CSS classes for rows, columns and cells.

like image 439
Christian Studer Avatar asked Jan 21 '10 09:01

Christian Studer


1 Answers

It seems like your question is pretty closely related to a little project I'm working on.

To solve this problem, I wrote htmlgen, mirrored on packagist -

use function htmlgen\html as h;
use function htmlgen\map;

$beeData = [
  'pop' => 'yup',
  'candy' => 'sometimes',
  'flowers' => 'so much',
  'water' => 'not really',
  'sand' => 'indifferent',
  'donuts' => 'most definitely'
];

echo h('table',
  h('thead',
    h('tr',
      h('td', 'item'),
      h('td', 'do bees like it?')
    )
  ),
  h('tbody',
    map($beeData, function($value, $key) { return
      h('tr',
        h('td', $key),
        h('td', $value)
      );
    })
  )
);

Output (whitespace not included in actual output)

<table>
  <thead>
    <tr>
      <td>item</td>
      <td>do bees like it?</td>
    </tr>
  </thead>
  <tbody>
   <tr>
     <td>pop</td>
     <td>yup</td>
   </tr>
   <tr>
     <td>candy</td>
     <td>sometimes</td>
   </tr>
   <tr>
     <td>flowers</td>
     <td>so much</td>
   </tr>
   <tr>
     <td>water</td>
     <td>not really</td>
   </tr>
   <tr>
     <td>sand</td>
     <td>indifferent</td>
   </tr>
   <tr>
     <td>donuts</td>
     <td>most definitely</td>
   </tr>
 </tbody>
</table>

Even though some of the examples seem like they might be somewhat verbose, I think the ability to utilize the DSL in a very programmatic way makes it very powerful.

If you are actually interested in creating your own library, I'd love to collaborate with you. Please check out the project and let me know what you think :)

like image 72
Mulan Avatar answered Nov 08 '22 19:11

Mulan