我已经研究了一个小时了。我放弃了。我试图在从MySQL表填充数组后创建一个数组。
然后,我想在表格顶部输出数组文本元素(表格标题)
$sqlRotations="SELECT id, rotationName FROM sched_rotations";
$resultRotations=mysql_query($sqlRotations);
$arrayRotations = array();
while($row_Rotation=mysql_fetch_assoc($resultRotations)){
$arrayRotations[]=$row_Rotation;
}
然后我试图将"rotationName"打印为表格列标题:
<table>
<tr>
<?
foreach( $arrayRotations as $key => $value){
echo "<td>Id: $key, Rotation:". $arrayRotations[$value]." </td>";
}
?>
</tr>
不幸的是,这在<t d>格式:
Id:0,旋转Id:1,旋转Id:2,旋转Id:3,旋转Id:4,旋转Id:5,旋转Id:6,旋转Id:7,旋转Id:8,旋转Id:9,旋转Id:10,旋转:
此外,如果我将for键值回波更改为:
foreach( $arrayRotations as $key => $value){
echo "<td>Id: $key, Rotation:". $value." </td>";
}
然后我得到这个输出:
Id:0,Rotation:Array Id:1,Rotation:Array Id:2,Rotation:Array Id:3,Rotation:Array Id:4,Rotaation:Array Id:5,Rotations:Array Id:6,Rotating:Array Id:7,Rotating:Array Id:8,Rotations:Array Id:9,Rotaion:Array Id:10,Rotational:Array
您使用foreach()
数组不正确。
<?php
foreach($arrayRotations as $line){
echo "<td>Id: " . $line['id'] . ' Rotation: '. $line['rotationName'] . '</td>';
}
?>
这就是您想要的吗?
<table>
<tr>
<th>Id</th>
<th>Rotation</th>
</tr>
<?php
foreach($arrayRotations as $rotation)
{
printf("<tr><td>%s</td> <td>%s</td></tr>", $rotation['id'], $rotation['rotationName');
}
?>
</table>