douzhang6176
2015-03-30 11:19 阅读 38
已采纳

php使用rowspan生成的表

I am using this code to generate a table which will subsequently be modified to upload data to a mysql database.

<?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; 
?>

At the moment the table generates 3 cells per iteration in 3 column cells,

| coordinates | sample ID | volume |

My question relates to how the php code can be altered so to generate a table where the coordinates cell can be arranged as a rowspan over the sampleID and volume cells positioned in 2 rows with the sample ID over the volume cell

|             | sample ID
| coordinates |------------
|             | volume
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • 已采纳
    donmqryh49993 donmqryh49993 2015-03-30 11:34

    try this,

    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>";
        }
    
    点赞 评论 复制链接分享
  • dongshuo1257 dongshuo1257 2015-03-30 11:32

    This works for me

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

    EDIT : fiddle

    点赞 评论 复制链接分享
  • douqi1931 douqi1931 2015-03-30 11:35

    you can set rowspan="2" on your coords TD.

    You really should not build your output like that. Try this approach:

    <?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>
    
    点赞 评论 复制链接分享

相关推荐