0

ポジションを 0 ではなく 1 に開始するには?

ファースト ネームは 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

すべての数値配列は、最初のインデックスとして 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 に答える