1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| <?php
$headers = array('c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8');
$body = array(
array('truc', 'truffe', 'truffe', 'truc', 'truc', 'truc', 'ok', 'ok'),
array('champ 1', 'champ 1', 'champ 2', 'champ 3', 'champ 3', 'champ 3', 'champ 4', 'champ 4'),
array('truc', 'truc', 'truffe', 'truffe', 'truc', 'ok', 'ok', 'ok')
);
class RowBuilder {
var $cells;
function RowBuilder($row) {
$this->cells = array();
foreach($row as $cell) {
$this->add($cell);
}
}
function _new($cell) {
$this->cells[] = array('content' => $cell, 'nb' => 1);
}
function add($cell) {
if (count($this->cells) == 0) {
$this->_new($cell);
} else {
$current = count($this->cells) - 1;
if ($this->cells[$current]['content'] == $cell) {
$this->cells[$current]['nb']++;
} else {
$this->_new($cell);
}
}
}
function display() {
echo '<tr>';
foreach($this->cells as $cell) {
echo '<td';
if ($cell['nb'] > 1) {
echo ' colspan="', $cell['nb'], '" ';
}
echo '>' , $cell['content'] , '</td>';
}
echo '</tr>';
}
}
//--- Affichage
echo '<table border="1">';
echo '<tr>';
foreach($headers as $header) {
echo '<th>' , $header , '</th>';
}
echo '</tr>';
foreach($body as $row) {
$row_builder = new RowBuilder($row);
$row_builder->display();
}
echo '</table>';
?> |