PHP 2中的动态行跨度表

时间:2018-11-06 20:14:32

标签: php html

我正在尝试在PHP中创建一个动态行跨度表。我有两个数据表 我要像这样:

Table Img

当前,我看到“第一列”的双精度值。在第一列上使用rowspan之后。但它看起来好像不是我需要的。

我的代码:

<table class="table">
  <thead class="thead-dark">
    <tr>
      <th scope="col">Room Type</th>
      <th scope="col">Sleeps</th>
      <th scope="col">Price for per nights</th>
      <th scope="col">Your Choices</th>
      <th scope="col">Select Rooms</th>
      <th scope="col"></th>
    </tr>
  </thead>

  <tbody>
    <?php
    $id = $_GET['id'];
    $query = "select * from tbl_hotelroomtype where hotelid='$id'";
    $post = $db->select($query);
    if($post){
        while($result = $post->fetch_assoc()){

    ?>

    <?php
    $id2 = $result['id'];
    $query2 = "select * from tbl_hotelroomtype2 where postid='$id2'";
    $post2 = $db->select($query2);
    if($post2){
        while($result2 = $post2->fetch_assoc()){
    ?>
    <tr>
      <td><?php echo $result['RoomType']; ?></td>
      <td><?php echo $result2['Sleep']; ?></td>
      <td><?php echo $result2['Price']; ?></td>
      <td><?php echo $result2['Choices']; ?></td>
      <td><?php echo $result2['Rooms']; ?></td>
      <td></td>
    </tr>
<?php } } ?>    
<?php } } ?>
  </tbody>
</table>

1 个答案:

答案 0 :(得分:0)

您应该可以使用rowspan,但是必须根据第二个结果集中的行数添加它,因此添加一些代码...

    if($post2){
        // Build the extra column from the text and use rowspan from the number of rows from last result set
        $extra = '<td rowspan="'.$post2->num_rows.'">'.$result['RoomType'].'</td>';
        while($result2 = $post2->fetch_assoc()){
    ?>
    <tr>
      // Add in extra td element (only done for the first row)
      <?php echo $extra; ?>
      <td><?php echo $result2['Sleep']; ?></td>
      <td><?php echo $result2['Price']; ?></td>
      <td><?php echo $result2['Choices']; ?></td>
      <td><?php echo $result2['Rooms']; ?></td>
      <td></td>
    </tr>
<?php 
    // Remove extra column so it doesn't get repeated
    $extra = ""; } } ?>    

我假设$post2->num_rows应该是行数,如果这样不能提供详细的信息,请检查此内容。