-1

我有这样的数组

Array  ([0] => 8 [1] => 8 [2] => 1 [3]=> 7 [4] => 8 )  

我在 PHP 上使用 ForEach 用上面的数组形成了表格行。但是当您查看上面的数组时,将在第 3 行创建 7 个 TD,在第 4 行创建 8 个 TD。我需要在行首移动第三行的空白 TD,而不是在行尾显示它。这可能吗?

<table border="1"><tbody>
  <tr align="right">
    <td align="right" colspan="0" rowspan="0" id="td_02">02</td>
    <td align="right" colspan="0" rowspan="0" id="td_06">06</td>
    <td align="right" colspan="0" rowspan="0" id="td_10">10</td>
    <td align="right" colspan="0" rowspan="0" id="td_14">14</td>
    <td align="right" colspan="0" rowspan="0" id="td_18">18</td>
    <td align="right" colspan="0" rowspan="0" id="td_22">22</td>
    <td align="right" colspan="0" rowspan="0" id="td_26">26</td>
    <td align="right" colspan="0" rowspan="0" id="td_30">30</td>
  </tr>
  <tr align="right">
    <td align="right" colspan="0" rowspan="0" id="td_01">01</td>
    <td align="right" colspan="0" rowspan="0" id="td_05">05</td>
    <td align="right" colspan="0" rowspan="0" id="td_09">09</td>
    <td align="right" colspan="0" rowspan="0" id="td_13">13</td>
    <td align="right" colspan="0" rowspan="0" id="td_17">17</td>
    <td align="right" colspan="0" rowspan="0" id="td_21">21</td>
    <td align="right" colspan="0" rowspan="0" id="td_25">25</td>
    <td align="right" colspan="0" rowspan="0" id="td_29">29</td>
  </tr>
  <tr align="right">
    <td>XX</td>
    <td>XX</td>
    <td>XX</td>
    <td>XX</td>
    <td>XX</td>
    <td>XX</td>
    <td>XX</td>
    <td>XX</td>
  </tr>
  <tr align="right">
    <td align="right" colspan="0" rowspan="0" id="td_03">03</td>
    <td align="right" colspan="0" rowspan="0" id="td_07">07</td>
    <td align="right" colspan="0" rowspan="0" id="td_11">11</td>
    <td align="right" colspan="0" rowspan="0" id="td_15">15</td>
    <td align="right" colspan="0" rowspan="0" id="td_19">19</td>
    <td align="right" colspan="0" rowspan="0" id="td_23">23</td>
    <td align="right" colspan="0" rowspan="0" id="td_27">27</td>
  </tr>
  <tr align="right">
    <td align="right" colspan="0" rowspan="0" id="td_31">31</td>
    <td align="right" colspan="0" rowspan="0" id="td_04">04</td>
    <td align="right" colspan="0" rowspan="0" id="td_08">08</td>
    <td align="right" colspan="0" rowspan="0" id="td_12">12</td>
    <td align="right" colspan="0" rowspan="0" id="td_16">16</td>
    <td align="right" colspan="0" rowspan="0" id="td_20">20</td>
    <td align="right" colspan="0" rowspan="0" id="td_24">24</td>
    <td align="right" colspan="0" rowspan="0" id="td_28">28</td>
  </tr>
</tbody></table>

4

1 回答 1

1

我完全不知道您是如何生成表格的,但是..

避免“空白”td 问题的一种方法是确保所有行都具有相同数量的 td,这可以通过查找max数组的值来完成。

例如:

<?php
    $row_array = array(8, 8, 1, 7, 8);
    $num_tds = max($row_array);
?>
<table border="1" cellpadding="3" cellspacing="3">
    <?php foreach($row_array as $row) : ?>
    <tr>
        <?php for ($i = 0; $i < $num_tds; $i++) : ?>
        <td>&nbsp;</td>
        <?php endfor; ?>
    </tr>
    <?php endforeach; ?>
</table>

在没有实际看到您的代码的情况下,我只是提供了盲目的解决方案,但我希望它有所帮助。

于 2012-11-24T10:24:52.157 回答