如何在php中禁用循环内部循环?

时间:2014-08-08 17:02:28

标签: php loops html-table

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

1 个答案:

答案 0 :(得分:0)

<?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++; } ?>