0

当我尝试动态地将行跨度赋予<td> (添加行跨度取决于数据库中的值)时,我得到的表格如下。出现了一个我不想要的额外列 (3::::3 ,4::::3)。如何阻止这个额外的列出现?

<?php
 $time = array (
  "06.00 - 07.00",
  "07.00 - 08.00",
  "08.00 - 09.00",
  "09.00 - 10.00",
  "10.00 - 11.00",
  "11.00 - 12.00",
  "12.00 - 01.00",
  "01.00 - 02.00",
  "02.00 - 03.00",
  "03.00 - 04.00"
 );
?>
<table border = "1">
 <tr><th>time</th><th>room1</th><th>room2</th><th>room3</th></tr>
<?php
 for ($i = 1; $i <= 10; $i++) {
?>
 <tr>
  <td><?php echo $time [$i - 1]; ?></td>
<?php
  for ($j = 1; $j <= 3; $j++) {
?>
  <td<?php if (($i == 2) && ($j == 3)) {echo ' rowspan="3"';} ?>><?php echo $i . "::::" . $j; ?></td>
<?php
  }
?>
 </tr>
<?php
 }
?>
</table>
4

1 回答 1

0

让我们破解你的代码...

您的数据

<?php
  $times  = array(
    "06.00 - 07.00",
    "07.00 - 08.00",
    "08.00 - 09.00",
    "09.00 - 10.00",
    "10.00 - 11.00",
    "11.00 - 12.00",
    "12.00 - 01.00",
    "01.00 - 02.00",
    "02.00 - 03.00",
    "03.00 - 04.00"
  );
?>

做一个foreach循环并渲染表格

<table border = "1">
  <tr>
    <th>time</th>
    <th>room1</th>
    <th>room2</th>
    <th>room3</th>
  </tr>
<?php
  $rowspan  = 3; // number of rows to cover
  $row  = 2; // starting from row number
  $col  = 3; // starting from col number

  foreach( $times as $i => $time ) {
    $i++;
    echo '<tr>';
    echo '<td>'.$time.'</td>';

    for( $j = 1; $j <= 3; $j++ ) {
      if ( $i === $row && $j === $col ) {
        echo '<td rowspan="'.$rowspan.'">'.$i."::::".$j.'</td>';
        continue;
      }
      if ( $i > $row && $i < ( $rowspan + $row ) && $j === $col ) {
        continue; // We don't need those extra columns, so continue
      }
      echo '<td>'.$i."::::".$j.'</td>';
    }

    echo '</tr>';
  }
?>
</table>

希望能帮助到你

于 2013-07-17T13:07:12.823 回答