2012-12-21 27 views
0

我一直在嘗試使用PHP在Yii中的表中創建行,但不知怎的,我無法做到這一點。我附上了下面的代碼。如何使用循環在PHP中創建表的行

PHP代碼:

<div class="grid_16"> 
<table style="width:100%"> 
<tr> 
<th style="width:33%;" id="wall_userNameStyle"><?php echo CHtml::activeLabel($Facility,'cname'); ?></th> 
<th style="width:33%;margin:0 0.5%" id="wall_userNameStyle"><?php echo CHtml::activeLabel($Facility,'cregistration'); ?></th> 
<th style="width:33%" id="wall_userNameStyle"><?php echo CHtml::activeLabel($Facility,'cannual'); ?></th> 
</tr> 

<?php $j=0; 
for($j; $j<5; $j++) 
{ 
?> 
<tr> 
<td style="width:33%;"><?php echo CHtml::activeTextField($Facility,"raymond[$j][0]",array('class' => 'edit_textbox','style' => 'width:99%')) ; ?></td> 

<td style="width:33%;margin:0 0.5%"><?php echo CHtml::activeTextField($Facility,"raymond[$j][1]",array('class' => 'edit_textbox','onkeypress'=>'return number_only(event)','style' => 'width:99%')) ; ?></td> 

<td style="width:33%;"><?php echo CHtml::activeTextField($Facility,"raymond[$j][2]",array('class' => 'edit_textbox','onkeypress'=>'return number_only(event)','style' => 'width:99%')) ; ?></td> 
</tr> 
</table> 
<?php } ?> 
</div> 
+5

你的''在你的循環中...把它帶到外面。 – Darrrrrren

回答

2

你需要的是

<?php 
for($j = 0; $j<5; $j++) 
{ 
?> 
<tr> 
<td style="width:33%;"><?php echo CHtml::activeTextField($Facility,"raymond[$j][0]",array('class' => 'edit_textbox','style' => 'width:99%')) ; ?></td> 

<td style="width:33%;margin:0 0.5%"><?php echo CHtml::activeTextField($Facility,"raymond[$j][1]",array('class' => 'edit_textbox','onkeypress'=>'return number_only(event)','style' => 'width:99%')) ; ?></td> 

<td style="width:33%;"><?php echo CHtml::activeTextField($Facility,"raymond[$j][2]",array('class' => 'edit_textbox','onkeypress'=>'return number_only(event)','style' => 'width:99%')) ; ?></td> 
</tr> 
<?php } ?> 

</table> 
</div> 

你不得不在其中關閉你的表早期循環側收盤</table>

+0

我很感謝你的幫助。是的,這是我沒有循環我想要的方式! –

3

您需要將</table>放在for循環之外。

1

你可以試試:

<table> 
<th> 
//information 
</th> 
<tr> 
<?php 
$j = 0; 
for($j = 0; $j<5; $j++) 
{ 
echo "<td>"; 
echo //table information 1 
echo "</td>"; 
echo "<td>"; 
echo //table information 2 
echo "</td>"; 
echo "<td>"; 
echo //table information 3 
echo "</td>"; 
} 
?> 
</tr> 
</table> 

表信息可以是任何東西,或任何款項。