如何动态增加所选区域数

hgtggwj0  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(297)

我的代码:

while($row = mysqli_fetch_assoc($result)){
    $fullname = $row['full_name'];
    $roll = $row['roll'];
    $s_result = $row['result'];
    $gm = $row['gm'];

    echo "<tr>";
    echo "<th scope='row'>1</th>";
    echo "<td>{$fullname}</td>";
    echo "<td>{$roll}</td>";
    echo "<td>{$s_result}</td>";
    echo "<td>$gm</td>";
    echo "</tr>";

}


我想增加所选的号码。现在是硬编码的。如何动态地增加数字?如果我输入更多的数据,那么我希望数字会自动增加。

xiozqbni

xiozqbni1#

你只需要做一点加法:

$counter = 1; // set a counter
while($row = mysqli_fetch_assoc($result)){
    $fullname = $row['full_name'];
    $roll = $row['roll'];
    $s_result = $row['result'];
    $gm = $row['gm'];

    echo "<tr>";
    echo "<th scope='row'>$counter</th>"; // use the counter
    echo "<td>{$fullname}</td>";
    echo "<td>{$roll}</td>";
    echo "<td>{$s_result}</td>";
    echo "<td>$gm</td>";
    echo "</tr>";
    $counter++; // increase the counter
}
baubqpgj

baubqpgj2#

你需要在上面取一个全局变量 while 循环并将其初始化为 1 然后按原样打印并在每次迭代中增加,它将按序列号打印。

$index = 1;
while($row = mysqli_fetch_assoc($result)){
    $fullname = $row['full_name'];
    $roll = $row['roll'];
    $s_result = $row['result'];
    $gm = $row['gm'];

    echo "<tr>";
    echo "<th scope='row'>$index</th>";
    echo "<td>{$fullname}</td>";
    echo "<td>{$roll}</td>";
    echo "<td>{$s_result}</td>";
    echo "<td>$gm</td>";
    echo "</tr>";

    $index = $index + 1;
}

相关问题