【问题标题】:How to disable looping inside do while loop in php?如何在php中禁用do while循环内的循环?
【发布时间】:2014-08-09 01:08:04
【问题描述】:

我使用 php 来生成 html 表格的行。这是我的代码:

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

以上代码生成的html源码为:

<tr>
 <td></td>
 <td rowspan="2" align="center">Internal<br />Grades</td>
 <td colspan="2">&nbsp;RHYMES</td>  
 <td align="center">B</td>    
</tr>
 <td rowspan="2" align="center">Internal<br />Grades</td>//I don't want this.
 <td colspan="2">&nbsp;CONVERSATION</td>  
 <td align="center">A</td>    
</tr>

我的预期输出是:

<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>

【问题讨论】:

  • 为什么不能在循环中移动&lt;tr&gt; 块,只在第一步创建一个rowspan 单元格?
  • 因为我的桌子看起来很糟糕。我的问题代码几乎是正确的,只是它循环了行跨度,但它不应该。所以我尝试了 continue 和 break ,这会使外观变得更糟。

标签: php loops html-table


【解决方案1】:
<?php $i=0; while ($row_grds=mysql_fetch_assoc($grds)){ ?>
    <tr>
        <td></td>
<?php if($i==0): ?>
        <td rowspan="<?php echo $cols; ?>" align="center">Internal<br />Grades</td>
<?php endif; ?>
        <td colspan="2">&nbsp;<?php echo strtoupper($row_grds['grade_name']);?></td>  
        <td align="center"><?php echo strtoupper($row_grds['igrade']);?></td> 
    </tr>
<?php $i++; } ?>

【讨论】:

  • 这将返回两行,但看起来好像是一行,因为所有内容都是跨越的。
  • 然后添加 和
    标签。我假设你有它们。
  • 是的,我有。但是您的代码仅返回两行中的一行数据,这些数据跨越并且看起来像一行。
  • 你可以制作带有输出的 jsFiddle 来检查它吗?除非您在 css 中更改了某些内容,否则它似乎不太可能发生故障。
  • 好的,我会的。感谢您的意见
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-21
  • 2021-07-27
  • 2011-03-13
  • 1970-01-01
  • 2021-07-18
  • 1970-01-01
相关资源
最近更新 更多