How to merge cells into php generated table?

I found a solution on this site to display an empty cell when the xml array is repeated, but I cannot find an answer to concatenate empty cells.

This is what I get:

This is what I would like it to look (in terms of merged cells):

So my question is how to merge these cells - tell the previous cell to use rowspan? (alternatively, is there a way to tell empty cells that they have no border?

Here is my code:

<tr> <?php if ($work_name !=$currentWork) { ?> <td> <?php if ($name != $currentName) { echo html_encode($name); } $currentName = $name; ?> </td> <td><i> <?php if ($work_name !=$currentWork) { echo html_encode($work_name); } else { echo " "; } $currentWork = $work_name; ?></i></td> <td> <?php if ($role_attrs->name == $previousRole) { continue; } else { echo html_encode($role_attrs->name); } $previousRole = $role_attrs->name; ?></td> </tr> 

I hope everything makes sense!

+4
source share
3 answers

alternatively, there is a way to tell empty cells that they have no border

It is much simpler, just add some class to an empty cell (for example, <td class="empty"> </td> and create it in your css using td.empty { border-top:none; }

+4
source

rowspan is definitely the way to go. However, instead of spitting out the HTML when you go, you will need to keep track of how many lines you need to cover, and then build the HTML accordingly.

This is an interesting problem without a simple solution. The problem is that rowspan should be in the first <td> , but you won to know how many lines to skip until you have processed them. Best to look at PHP & rsquo; The document object model . The DOM will allow you to build a table and, if necessary, configure the rowspan attribute.

A few places to run:

Update

This should illustrate what is involved.

 <html> <head> <title>My Sample</title> <style type="text/css"> body { font: normal 62.5% sans-serif; } table { font-size: 1.3em; width: 420px; border: 0; border-collapse:collapse; } td { vertical-align: top; border-bottom: 1px solid #000; } </style> </head> <body> <?php $works = array( array( 'composer' => 'HAYDN, Joseph', 'title' => 'Die Schöpfung', 'role' => 'Adam' ), array( 'composer' => 'HAYDN, Joseph', 'title' => 'Die Schöpfung', 'role' => 'Raphäel' ), array( 'composer' => 'HAYDN, Joseph', 'title' => 'Il mondo della luna', 'role' => 'Ernesto' ), array( 'composer' => 'HAYDN, Joseph', 'title' => 'La vera costanza', 'role' => 'Masino' ), array( 'composer' => 'MENOTTI, Gian Carlo', 'title' => 'The Telephone', 'role' => 'Ben' ), array( 'composer' => 'MOORE, Douglas', 'title' => 'Gallantry', 'role' => 'Dr Gregg' ), array( 'composer' => 'MOZART, Wolfgang Amadeus', 'title' => 'Die Zauberflöte', 'role' => 'Der Sprecher' ), array( 'composer' => 'MOZART, Wolfgang Amadeus', 'title' => 'Die Zauberflöte', 'role' => 'Papageno' ), array( 'composer' => 'MOZART, Wolfgang Amadeus', 'title' => 'La nozze di Figaro', 'role' => 'Il conte Almaviva' ), array( 'composer' => 'NICHOLSON, Alisdair', 'title' => 'Two sisters, a rose, a flood and snow', 'role' => 'baritone' ), array( 'composer' => 'OFFENBACH, Jacques', 'title' => 'Barbe-bleue', 'role' => 'Graf Oscar' ), array( 'composer' => 'OFFENBACH, Jacques', 'title' => 'La vie parisienne', 'role' => 'Bobinet' ), array( 'composer' => 'PUCCINI, Giacomo', 'title' => 'La bohème', 'role' => 'Schaunard' ), array( 'composer' => 'PUCCINI, Giacomo', 'title' => 'Madama Butterfly', 'role' => 'Il principe' ), array( 'composer' => 'PUCCINI, Giacomo', 'title' => 'Madama Butterfly', 'role' => 'Yamadori' ) ); $current_composer = ''; $current_title = ''; $composer_row_count = 1; $title_row_count = 1; $doc = new DOMDocument(); $table = $doc->createElement('table'); $doc->appendChild($table); foreach($works as $work) { $row = $doc->createElement('tr'); $table->appendChild($row); if ($current_composer !== $work['composer']) { $current_composer = $work['composer']; $composer_row_count = 1; $composer_col = $doc->createElement('td', $work['composer']); $row->appendChild($composer_col); } else { $composer_col->setAttribute('rowspan', ++$composer_row_count); } if ($current_title !== $work['title']) { $current_title = $work['title']; $title_row_count = 1; $title_col = $doc->createElement('td', $work['title']); $row->appendChild($title_col); } else { $title_col->setAttribute('rowspan', ++$title_row_count); } $role_col = $doc->createElement('td', $work['role']); $row->appendChild($role_col); } echo $doc->saveHTML(); ?> </body> </html> 

Copy the code and run it. The layout is exactly what you asked for, but naturally, you will need to adapt it to your needs.

+2
source
 <tr> <td colspan="2">a cell using 2 colums</td> </tr> <tr> <td rowspan="2">a cell using 2 rows</td> </tr> 

Hope that helps

0
source

All Articles