tags:

views:

252

answers:

2

Hi!

I need to read a bi-dimensional array and convert the values into an HTML table. For example:

$grid = array( array(0,0,0,0),array(0,0,0,0),array(0,0,0,0),array(0,0,0,0),array(0,0,0,0));
$grid[0][0]=4;$grid[0][1]=4;$grid[0][2]=4;$grid[0][3]=4;
$grid[1][0]=3;$grid[1][1]=3;$grid[1][2]=5;$grid[1][3]=5;
$grid[2][0]=3;$grid[2][1]=3;$grid[2][2]=5;$grid[2][3]=5;
$grid[3][0]=0;$grid[3][1]=0;$grid[3][2]=5;$grid[3][3]=5;

Reading this array, I want to dynamically write the <TABLE> syntax where the cells with the same number in the grid are merged horizontally or vertically:

That is, I should be able to create the following syntax from the $grid above:

<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td colspan="4">4</td>
</tr><tr>
<td colspan="2">3</td>
<td colspan="2" rowspan="2">5</td>
</tr><tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr></table>

I have been able to process the colspan correctly, but am having trouble with the rowspan issue. Any ideas? basically I want to be able to create custom homepages with custom content from the array "format" established by the $grid.

Any help greatly appreciated!

A: 
<?php
$grid = array( array(0,0,0,0),array(0,0,0,0),array(0,0,0,0),array(0,0,0,0),array(0,0,0,0)); 

foreach($grid AS $rowkey => $row)
{
  echo "<tr>\n";
  foreach($grid[$rowkey] AS $columnKey => $field)
  {
    echo "<td>{$field}</td>\n";
  }
  echo "</tr>\n";
}
?>
Patrick McGovern
This creates the table but does not handle merging cells vertically and horizontally based on the value of $grid[x][y]!
+1  A: 

Code:

<?php

$grid = array(
    array(0,0,0,0),
    array(0,0,0,0),
    array(0,0,0,0),
    array(0,0,0,0),
);

$grid[0][0]=4;$grid[0][1]=4;$grid[0][2]=4;$grid[0][3]=4;
$grid[1][0]=3;$grid[1][1]=3;$grid[1][2]=5;$grid[1][3]=5;
$grid[2][0]=3;$grid[2][1]=3;$grid[2][2]=5;$grid[2][3]=5;
$grid[3][0]=0;$grid[3][1]=0;$grid[3][2]=5;$grid[3][3]=5;

$w = count($grid[0]);
$h = count($grid);

for ($y = 0; $y < $h; ++$y) {
    echo "<tr>\n";

    for ($x = 0; $x < $w; ++$x) {
        $value = $grid[$y][$x];

        if ($value === null) {
            continue;
        }

        $colspan = 1;

        while ($x + $colspan < $w && $grid[$y][$x + $colspan] === $value) {
            $grid[$y][$x + $colspan++] = null;
        }

        $rowspan    = 1;
        $rowMatches = true;

        while ($rowMatches && $y + $rowspan < $h) {
            for ($i = 0; $i < $colspan; ++$i) {
                if ($grid[$y + $rowspan][$x + $i] !== $value) {
                    $rowMatches = false;
                    break;
                }
            }

            if ($rowMatches) {
                for ($i = 0; $i < $colspan; ++$i) {
                    $grid[$y + $rowspan][$x + $i] = null;
                }

                ++$rowspan;
            }
        }

        $rowspan = $rowspan > 1 ? " rowspan=\"$rowspan\"" : "";
        $colspan = $colspan > 1 ? " colspan=\"$colspan\"" : "";

        echo "  <td$rowspan$colspan>$value</td>\n";
    }

    echo "</tr>\n";
}

?>

Output:

<tr>
  <td colspan="4">4</td>
</tr>
<tr>
  <td rowspan="2" colspan="2">3</td>
  <td rowspan="3" colspan="2">5</td>
</tr>
<tr>
</tr>
<tr>
  <td colspan="2">0</td>
</tr>
John Kugelman
Excellent! Thanks!!!