如何将位置设为1而不是0?
因为名字从0开始。我所要做的就是让名字从1开始,到50结束。
有什么方法可以解决这个问题吗?
这是我的代码:
<html>
<head>
<title>SEATPLAN</title>
</head>
<body>
<table border = "2" cellpadding = "20" cellspacing = "10">
<tr>
<td colspan = 5 rowspan = 2> </td>
<td align = "center"> Teachers Table</td>
<td colspan = 5 rowspan = 2> </td>
</tr>
<tr>
<td colspan = 1 rowspan = 6 width="1000"> </td>
</tr>
<?php
$names = array('Acog','Alaya-ay','Anino','Balsa','Baron','Borda','Bravo','Dalagan','Detumal','Enriquez',
'Hernane','Jose','Laminero','Montilla','Moraclo','Ogang','Palencia','Palencia','Pandili',
'Ramo','Ravelo','Septio','Tapel','Tayone','Trinidad','Yntong','Student','Student','Student',
'Student','Student','Student','Student','Student','Student','Student','Student','Student',
'Student','Student','Student','Student','Student','Student','Student','Student','Student'
,'Student','Student','Student');
?>
<?php
foreach($names as $position => $name){
echo "<td width='500' align='center'>".$position."<br>".$name."<br/>";
if ($position == 9){
echo "<tr width='500' align='center'>"."<br/>";}
if ($position == 19){
echo "<tr width='500' align='center'>"."<br/>";}
if ($position == 29){
echo "<tr width='500' align='center'>"."<br/>";}
if ($position == 39){
echo "<tr width='500' align='center'>"."<br/>";}
}
?>
</table>
</body>
</html>
答案 0 :(得分:3)
如果您以后想要将密钥($position
)用于其他任何事情,这是最简单的解决方案:
echo "<td width='500' align='center'>".($position+1)."<br>".$name."<br/>";
答案 1 :(得分:0)
我会用:
$i = 1;
foreach($names as $name) {
echo '<td>'. $i .': '. $name .'</td>';
if($i % 10 == 0)
echo '</tr><tr>';
$i++;
}
答案 2 :(得分:0)
只需添加一个新变量并使用$position + 1
初始化它。
foreach($names as $position => $name){
$newPosition = $position + 1;
echo "<td width='500' align='center'>".$newPosition."<br>".$name."<br/>";
.
.
}
这也将保留当前$position
的值。
答案 3 :(得分:0)
所有数字数组都以0开头,因为它是第一个索引。
只是做这个技巧,它不会改变数组结构,但会显示你想要的东西:
foreach($names as $position => $name){
echo "<td width='500' align='center'>".($position+1)."<br>".$name."<br/>";
// rest of the code
}
因此它总是会将1添加到实际位置。