如何动态增加所选区域号

时间:2018-10-29 16:33:05

标签: php mysql mysqli

我的代码:

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>";

}

The Image

我想增加选择的数字。现在,它已被硬编码。如何动态增加数量?如果我输入更多数据,那么我希望数字会自动增加。

2 个答案:

答案 0 :(得分:2)

您只需要做一些补充:

$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
}

答案 1 :(得分:1)

您需要在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;
}