开发者

How to merge cells in php generated table?

开发者 https://www.devze.com 2023-04-13 02:07 出处:网络
I found the solution on this site for displaying a blank cell when the xml array is repeated, but I can\'t seem to find an answer to merging the blank cells.

I found the solution on this site for displaying a blank cell when the xml array is repeated, but I can't seem to find an answer to merging the blank cells.

This is what I'm getting:

How to merge cells in php generated table?

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

How to merge cells in php generated table?

So my question is, how do I merge those cells - tell the previous cell to use a rowspan? (alternatively, is there a way to tell the empty cells to 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_encod开发者_如何学Goe($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 that all makes sense!


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

It's much easier way, just add some class to empty cell (like <td class="empty"> </td> and style it in your css with td.empty { border-top:none; }


rowspan is certainly the way to go. However, rather than spitting the HTML out as you go, you’ll need to keep track of how many rows to span and then construct the HTML accordingly.

This is an interesting problem without an easy solution. The problem is that rowspan has to reside in the first <td>, but you won’t know how many rows to span until after you’ve processed them. Your best bet is to look into PHP’s Document Object Model. The DOM will allow you to construct your table and adjust the rowspan attribute as needed.

A few places to start:

  • HTML DOM Tutorial
  • Generating (X)HTML Documents Using DOMDocument In PHP

Update

This should illustrate what’s 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’ll need to adjust it to your needs.


<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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号