0

如何开始位置为 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>
4

4 回答 4

3

如果您以后想将密钥 ( $position) 用于其他任何事情,这是最简单的解决方案:

echo "<td width='500' align='center'>".($position+1)."<br>".$name."<br/>";
于 2013-09-11T11:09:37.727 回答
0

只需添加一个新变量并使用$position + 1.

foreach($names as $position => $name){
    $newPosition = $position + 1;
    echo "<td width='500' align='center'>".$newPosition."<br>".$name."<br/>";
    .
    .
}

这也将保留 current 的值$position

于 2013-09-11T11:11:42.263 回答
0

所有数值数组都以 0 开头,因为它是第一个索引。

只要做到这一点,它不会改变数组结构,但会显示你想要的:

foreach($names as $position => $name){
    echo "<td width='500' align='center'>".($position+1)."<br>".$name."<br/>";
    // rest of the code
}

所以它总是会在实际位置上加 1。

于 2013-09-11T11:14:14.230 回答
0

我会使用:

$i = 1;
foreach($names as $name) {
     echo '<td>'. $i .': '. $name .'</td>';
     if($i % 10 == 0)
         echo '</tr><tr>';
     $i++;
}
于 2013-09-11T11:09:25.600 回答