php 使用 rowspan 生成 table

php generated table using rowspan

我正在使用此代码生成一个 table,随后将对其进行修改以将数据上传到 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; 
?>

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

| coordinates | sample ID | volume |

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

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

这对我有用

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

编辑:fiddle

试试这个,

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>";
    }

您可以在您的坐标 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>