【问题标题】:How to automatically generate rowspan using php如何使用php自动生成rowspan
【发布时间】:2017-07-26 05:58:41
【问题描述】:

在这段代码中,一切都很完美,除了我无法在内部成绩单元格中生成行跨度。这条线<td rowspan="<?php echo $cols;?>"> 隐藏了底部边框,而我希望每次代码生成行时都会跨越这些行。感谢您的输入,很抱歉使用mysql() 功能。我是新手。

<?php $cols=mysql_num_rows($grds);  ?>
<table>
<?php do{ ?>
<tr>
<td>&nbsp;</td>
//This hide the bottom border
<td rowspan="<?php echo $cols;?>">Internal<br />Grades</td>
<td colspan="2">&nbsp;<?php echo strtoupper($row_grds['grade_name']); ?></td>  
<td><?php echo strtoupper($row_grds['igrade']); ?></td>
<?php } while ($row_grds=mysql_fetch_assoc($grds)); ?>
</tr>
</table>

输出如下: output is here

php生成的代码是:

<table>
   <tr>
  <td>&nbsp;</td>
  <td rowspan="1">&nbsp;</td>// A
  <td colspan="2">&nbsp;RHYMES</td>  
  <td align="center">B</td>
   </tr>
      <tr>
  <td>&nbsp;</td>
  <td rowspan="2">&nbsp;</td>//B
  <td colspan="2">&nbsp;CONVERSATION</td>  
  <td align="center">A</td>
   </tr>
</table>

我想合并 A 和 B

预期输出是:

<table>
  <tr>
    <td>&nbsp;</td>
    <td rowspan="2">&nbsp;Internal<br /> Grade</td>
    <td colspan="2">&nbsp;RHYMES</td>  
    <td align="center">B</td>
    </tr>
        <tr>
    <td>&nbsp;</td>
      <td colspan="2">&nbsp;CONVERSATION</td>  
    <td align="center">A</td>
  </tr>
</table>

【问题讨论】:

  • 你能展示一下它产生了什么吗?实际的 HTML 输出会更容易。首先将您的结束 &lt;/tr&gt; inside while 循环。
  • @outvande,我将创建一个图像并更新问题。
  • 不是图片,只是发布实际的 HTML
  • 你知道rowspancolspan 做对了吗?您的colspan 似乎毫无意义,因为两行中的td 都有它。底行的rowspan="2" 也不起作用。
  • @Gasim,我想你误解了我的问题。我正在使用 php 从数据库中生成行。所以我没有再放 而是 php 生成它们。那么我如何摆脱这个额外的行以使其正确。谢谢

标签: php loops html-table


【解决方案1】:

你可以做类似这样的事情:

<table>
<?php $row_grds = mysql_fetch_assoc($grds); ?>
   <tr>
     <td>&nbsp;</td>
     <td rowspan="<?=$cols;?>">Internal<br />Grades</td>
     <td colspan="2">&nbsp;<?=strtoupper($row_grds['grade_name']);?></td>  
     <td><?=strtoupper($row_grds['igrade']);?></td>
    </tr>

<?php while ($row_grds=mysql_fetch_assoc($grds)): ?>
   <tr>
     <td>&nbsp;</td>
     <td colspan="2">&nbsp;<?=strtoupper($row_grds['grade_name']);?></td>  
     <td><?=strtoupper($row_grds['igrade']);?></td>
    </tr>
<?php endwhile; ?>

它并不完美,但您使用的是mysql_fetch_assoc,它本身也不完美:D

【讨论】:

  • 非常感谢。但是您的代码不起作用,它只返回一行和一列。感谢您的意见。
  • 我想我不明白你想做什么。查看这段代码的输出(我没有数据库,所以我使用数组模拟它):ideone.com/F6PXAt;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-02
  • 1970-01-01
  • 2023-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-08
相关资源
最近更新 更多