2013-07-17 27 views
0

當我嘗試將rowspan動態添加到<td> (添加rowspan取決於db中的值)時,我得到如下表。 出現一個額外的列(3 :::: 3,4 :::: 3),我不想要。 如何阻止出現這個額外的列?如何動態更改colspan和rowspan,而不會對錶結構造成損害

<?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> 
+1

你可以嘗試修復你的代碼/問題的格式有點?這是一列火車殘骸。 –

+0

實際上我想上傳表格圖像。 – gayathri

+0

,但它不允許我上傳 – gayathri

回答

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> 

希望它有幫助

+0

非常感謝,它幫助我獲得解決方案。 – gayathri

+0

歡迎您:) – bystwn22