Re: [PHP] Creating a table with merged cells



On Thu, 2007-08-23 at 15:59 +0200, Phpmanni wrote:
Hi
I need to represent a sort of map. The map is a rectangular
table of size X in widht and Y in height, so that I have X*Y
square cells. I need to record in a database some infos for
each cell.
This is easy, I thought to use a record that is something
like (X,Y,MyData), so with two nested loops I can create the
HTML table with data in it.
The problem is that I need to join adjacent cells, so that I
can obtain bigger cells made of base little square cells.
So, starting from a table like this (see with fixed size font)
+-+-+-+-+-+-+-+-+-+
| | | | | | | | | |
+-+-+-+-+-+-+-+-+-+
| | | | | | | | | |
+-+-+-+-+-+-+-+-+-+
| | | | | | | | | |
+-+-+-+-+-+-+-+-+-+

I must find a way to store in the database a table like this
+-+-+-+-+-+-+-+-+-+
| 2 | | | |4 cells|
+-+-+-+-+-+-+-+-+-+
| |BIGBIGB| | | | |
+-+BIGBIGB+-+-+-+-+
| |BIGBIGB| | | | |
+-+-+-+-+-+-+-+-+-+

I thougt to use a record like (X,Y,XWIDTH,YHEIGHT,DATA), but
I cannot imagine the way to create the resulting HTML table.
Using graphics will be a lot easyer, but I must insert
combos and checkboxes into the cells, so I must use html...
Any ideas?

HTML tables are encoded in HTML from top row to bottom row and left cell
to right cell. As such I would suggest storing in the database the
colspan and rowspan for any given cell. Then when retrieving the cells
you can loop through and use colspan and rowspan as necessary with a
little bit of bookkeeping to know when to skip. For instance, let's say
you have the exact table above:

<?php

// $cells = result_of_magical_query(); // ordered by y, then x

$cells = array();

$cells[] = array( 'x' => 0, 'y' => 0, 'cols' => 2, 'rows' =>
1, 'data' => '2', );
$cells[] = array( 'x' => 2, 'y' => 0, 'cols' => 1, 'rows' =>
1, 'data' => '', );
$cells[] = array( 'x' => 3, 'y' => 0, 'cols' => 1, 'rows' =>
1, 'data' => '', );
$cells[] = array( 'x' => 4, 'y' => 0, 'cols' => 1, 'rows' =>
1, 'data' => '', );
$cells[] = array( 'x' => 5, 'y' => 0, 'cols' => 4, 'rows' =>
1, 'data' => '4 cells', );

$cells[] = array( 'x' => 0, 'y' => 1, 'cols' => 1, 'rows' =>
1, 'data' => '', );
$cells[] = array( 'x' => 1, 'y' => 1, 'cols' => 4, 'rows' =>
2, 'data' => 'BIGBIGB<br />BIGBIGB<br />BIGBIGB', );
$cells[] = array( 'x' => 5, 'y' => 1, 'cols' => 1, 'rows' =>
1, 'data' => '', );
$cells[] = array( 'x' => 6, 'y' => 1, 'cols' => 1, 'rows' =>
1, 'data' => '', );
$cells[] = array( 'x' => 7, 'y' => 1, 'cols' => 1, 'rows' =>
1, 'data' => '', );
$cells[] = array( 'x' => 8, 'y' => 1, 'cols' => 1, 'rows' =>
1, 'data' => '', );

$cells[] = array( 'x' => 0, 'y' => 2, 'cols' => 1, 'rows' =>
1, 'data' => '', );
$cells[] = array( 'x' => 5, 'y' => 2, 'cols' => 1, 'rows' =>
1, 'data' => '', );
$cells[] = array( 'x' => 6, 'y' => 2, 'cols' => 1, 'rows' =>
1, 'data' => '', );
$cells[] = array( 'x' => 7, 'y' => 2, 'cols' => 1, 'rows' =>
1, 'data' => '', );
$cells[] = array( 'x' => 8, 'y' => 2, 'cols' => 1, 'rows' =>
1, 'data' => '', );

$skip = array();
$rows = array();
foreach( $cells as $cell )
{
if( $cell['cols'] < 1 )
{
$cell['cols'] = 1;
}

if( $cell['rows'] < 1 )
{
$cell['rows'] = 1;
}

if( isset( $skip[$cell['y']][$cell['x']] ) )
{
continue;
}

$data = $cell['data'];
if( trim( (string)$data ) === '' )
{
$data = '&nbsp;';
}

$rows[$cell['y']][$cell['x']] =
'<td'
. ($cell['cols'] > 1 ? ' colspan="'.$cell['cols'].'"' :
'')
. ($cell['rows'] > 1 ? ' rowspan="'.$cell['rows'].'"' :
'')
.'>'
.$data
.'</td>';

for( $i = 0; $i < $cell['rows']; $i++ )
{
for( $j = 0; $j < $cell['cols']; $j++ )
{
$skip[$cell['y'] + $i][$cell['x'] + $j] = true;
}
}
}

foreach( array_keys( $rows ) as $rowIndex )
{
$rows[$rowIndex] = implode( '', $rows[$rowIndex] );
}

$table = '<table border="1">';

if( $rows )
{
$table .=
'<tr>'.implode( '</tr><tr>', $rows ).'</tr>';
}

$table .= '</table>';

echo $table;
?>

Cheers,
Rob
--
............................................................
SwarmBuy.com - http://www.swarmbuy.com

Leveraging the buying power of the masses!
............................................................
.



Relevant Pages

  • Re: WebCustomControl or DataGrid
    ... The reason for this is that ASP.Net is all about HTML and web sites. ... And you want to be able to disable cells. ... Now you have control over which buttons are enabled disabled, ... > textboxes because I found it to be impossible to use the datagrid ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Using HTML_Table
    ... Currently I am using the script below, ... database to display the table info in an HTML table. ... Basically, one of the good things about HTML_Table is that you no longer need to think of tables as cells, instead you can think of it as rows, columns, cells, and all sorts of things inbetween. ...
    (comp.lang.php)
  • Re: unhelpful MVP
    ... which one would have thought very pertinent to this forum, ... many Web sites are designed to include navigation links down ... implemented in one or more HTML table cells. ... a simple rearrangement of table cells can move your body ...
    (microsoft.public.publisher.webdesign)
  • Re: unhelpful MVP
    ... many Web sites are designed to include navigation links down ... implemented in one or more HTML table cells. ... a simple rearrangement of table cells can move your body ... Which was recomended by this forum! ...
    (microsoft.public.publisher)
  • Re: unhelpful MVP
    ... Please do not bash David. ... navigation links down ... >implemented in one or more HTML table cells. ...
    (microsoft.public.publisher)