我在这个网站上找到了解决方案,当重复xml数组时显示一个空白单元格,但我似乎无法找到合并空白单元格的答案。
这就是我得到的:
这就是我想要的样子(就合并的单元而言):
所以我的问题是,如何合并这些单元格 - 告诉前一个单元格使用rowspan? (或者,有没有办法告诉空单元格没有边框?
这是我的代码:
<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>
我希望一切都有意义!
答案 0 :(得分:4)
或者,有没有办法告诉空单元格没有边框
这是更简单的方法,只需将一些类添加到空单元格(如<td class="empty"> </td>
,并使用td.empty { border-top:none; }
答案 1 :(得分:2)
rowspan
肯定是要走的路。但是,您不必随意吐出HTML,而是需要跟踪要跨越的行数,然后相应地构建HTML。
如果没有 easy 解决方案,这是一个有趣的问题。问题是rowspan
必须驻留在第一个<td>
中,但在之后处理它们之前,您将不知道要跨越多少行。最好的办法是研究PHP的Document Object Model。 DOM允许您构建表并根据需要调整rowspan
属性。
开始的几个地方:
这应该说明所涉及的内容。
<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>
复制代码并运行它。布局正是您所要求的,但自然您需要根据需要进行调整。
答案 2 :(得分:0)
<tr>
<td colspan="2">a cell using 2 colums</td>
</tr>
<tr>
<td rowspan="2">a cell using 2 rows</td>
</tr>
希望有所帮助