php使用rowspan生成的表


php generated table using rowspan

我正在使用此代码生成一个表,该表随后将被修改以将数据上传到mysql数据库。

<?php
$start_loc_number= 1 ;
$start_loc_alpha= 'A' ;
$end_loc_number= 10 ;
$end_loc_alpha= 'J' ;
$out = ''; 
   $out .= '<table border = 1 bordercolor="#FF0000">';
    for($tr='A';$tr<=$end_loc_alpha;$tr++)
    { $out .= "<tr>";
        for($td=1;$td<=$end_loc_number;$td++)
        { $out .= '<td BGCOLOR="#99CCFF">'.$tr.$td.'</td>
                                 <td id="sampleID" contenteditable="true">  sampleID</td>
                                 <td id="volume" contenteditable="true">  volume</td>'  ;
        }
    $out .= "</tr>";
    }
    $out .= "</table>";
echo $out; 
?>

目前,该表在3列单元格中每次迭代生成3个单元格,

| coordinates | sample ID | volume |

我的问题涉及如何更改php代码以生成一个表,其中坐标单元格可以排列为sampleID上的行跨度,体积单元格位于2行中,样本ID位于体积单元格上

|             | sample ID
| coordinates |------------
|             | volume

试试这个,

for($tr='A';$tr<=$end_loc_alpha;$tr++)
    { $out .= "<tr>";
        for($td=1;$td<=$end_loc_number;$td++)
        { $out .= '<td BGCOLOR="#99CCFF">'.$tr.$td.'</td><td>
                                <table><tr style="border-bottom: 1px solid red">
                                 <td id="sampleID" contenteditable="true">  sampleID</td></tr>
                                <tr> <td id="volume" contenteditable="true">  volume</td></tr></table></td>'  ;
        }
    $out .= "</tr>";
    }

这适用于我的

<table>
    <tr>
        <td rowspan="2">test</td>
        <td>test1</td>
    </tr>
    <tr>
        <td>test2</td>
    </tr>
</table>

编辑:小提琴

您可以在coords TD.上设置rowspan="2"

你真的不应该那样构建你的输出。尝试这种方法:

<?php
$content = array(
    array(
        'cords' => 1,
        'samp' => "Bar",
        'vol' => 13
    ),
    array(
        'cords' => 2,
        'samp' => "Foo",
        'vol' => 456
    ),
    array(
        'cords' => 3,
        'samp' => "DJ",
        'vol' => 34
    )
);
?>
<table>
    <tbody>
        <? foreach($content as $key => $value) { ?>
        <tr>
            <th rowspan="2">Coordinates: <?= $value['cords'] ?></th>
            <td>sample ID: <?= $value['samp'] ?></td>
        </tr>
        <tr>
            <td>volume: <?= $value['vol'] ?></td>
        </tr>
        <? } ?>
    </tbody>
</table>