如何在表格末尾添加一个额外的单元格?

时间:2018-11-20 02:33:09

标签: php html html-table

我有一张学生及其成绩表。我想添加一个字段,允许用户为最后一列添加新的作业和成绩。我有

echo "<td><div contenteditable='true' placeholder='".$row2['Student Name']."'></div></td>";

放入最后一列,但仅将其添加到我的最后一个单元格中,就会出现一个错误,如下所示 in this picture (follow the link I can't embed photos yet).

我的代码如下

$last_stud = null;

    while($row2 = mysqli_fetch_assoc($result)){
        if($last_stud != $row2['Student Name']){

            // close previous <tr>
            if ( $last_stud !== null ) {
                echo "<td><div contenteditable='true' placeholder='".$row2['Student Name']."'></div></td>";
                echo '</tr>';
            }

            $last_stud = $row2['Student Name'];
            echo"<tr><td>{$row2['Student Name']}</td>";
            echo"<td>{$row2['grade']}</td>";
        } else {
            echo"<td>{$row2['grade']}</td>";
        }
    }
    echo '</tr>';

如何更改此代码以纠正一个错误?

1 个答案:

答案 0 :(得分:0)

您应该使用$last_stud而不是$row2['Student Name'],因为您想要的是前一个学生,而不是当前学生。

echo "<td><div contenteditable='true' placeholder='".$last_stud."'></div></td>";

对于最后一个学生,您还需要在循环后 内添加它-

}
echo "<td><div contenteditable='true' placeholder='".$last_stud."'></div></td>";
echo '</tr>';
相关问题