[英]HTML Table does not display MySQL data correctly
我运行多个MySQL查询,并基于变量(例如人员的功能等),它应将数据输出到HTML表中的行和列中。 但是,在运行代码时,最终会陷入一团糟,数据没有跟随正确的列。 有关更多信息,请参见下图( https://imgur.com/alRkog8 )。 我似乎无法弄清楚我在做什么错,因为在我看来我已经正确放置了所有的“ tr”和“ td”。
session_start();
if(!isset($_SESSION['user1'])){
header("location:".WEBROOT."index.php");
exit();
}
include_once DOCROOT . 'DBconnect.php';
?>
<html>
<head>
<?php
$title = "Participants";
$file = __FILE__;
include DOCROOT . 'pages/menu.php';
?>
<div id="content">
<div class="widecontentitem" id="lastitem">
<?php
$result = mysqli_query($db_link, 'SELECT institutionID FROM users WHERE users.UserID = \'' . $_SESSION['user1'] . '\'');
if (!$result) {
die('Invalid query: ' . mysqli_error());
}
$institutionID = "";
while ($row = mysqli_fetch_array($result, MYSQLI_NUM)) {
$institutionID = $row[0];
}
print "<input type=\"hidden\" id=\"institution\" value=\"" . $institutionID . "\"> ";
$result = mysqli_query($db_link, 'SELECT participants.name, participants.surname, participants.email, participants.special, participants.housingcomments, participants.functionID, participants.infix, participants.committee, participants.dateOfBirth, participants.housing, participants.countryID, participants.participantID FROM participants WHERE participants.institutionID =' . $institutionID);
if (!$result) {
die('Invalid query:2 ' . mysqli_error());
}
print "<table>";
print "<tr><th><b>Function</b></th><th><b>Name</b></th><th><b>Surname</b></th><th><b>Date Of Birth</b></th><th><b>Country</b></th><th><b>Committee</b></th></tr>";
while ($row = mysqli_fetch_array($result, MYSQLI_NUM)) {
if($row[9]==1){
$row[0] = "(H)" . $row[0];
}
if( $row[5]==4){
$title = "Delegate ";
$result3 = mysqli_query($db_link, 'SELECT delegation FROM delegations WHERE ambassadorID =' . $row[11] );
$num_rows = mysqli_num_rows($result3);
if($num_rows==1){
$title = "Ambassador ";
}
$result2 = mysqli_query($db_link, 'SELECT delegation FROM delegations WHERE delegationID = ' . $row[10] );
while ($row2 = mysqli_fetch_array($result2, MYSQLI_NUM)) {
print "<tr><td>$title </td><td>$row[0] </td><td>$row[1] </td><td>$row[8] </td><td>$row2[0] </td><td>$row[7] </td></tr>";
}
}
if( $row[5]==2){
print "<tr><td>MUN-Director </td><td>$row[0] </td><td>$row[1] </td><td>$row[8] </td><td></td><td></td></tr>";
}
if($row[5]==5){
print "<tr><td>President </td><td>$row[0] </td><td>$row[1] </td><td>$row[8] </td><td></td><td>$row[7] </td></tr>";
}
if($row[5]==6){
print "<tr><td>Deputy President </td><td>$row[0] </td><td>$row[1] </td><td>$row[8] </td><td></td><td>$row[7] </td></tr>";
}
if($row[5]==7){
print "<tr><td>Chair </td><td>$row[0] </td><td>$row[1] </td><td>$row[8] </td><td></td><td>$row[7] </td></tr>";
}
if($row[5]==8){
print "<tr><td>Deputy Chair </td><td>$row[0] </td><td>$row[1] </td><td>$row[8] </td><td></td><td>$row[7] </td></tr>";
}
if($row[4]!=NULL || $row[3]!=NULL){
print "<table>";
if($row[3]!=NULL){
print "<tr><td colspan=\"6\">Comments: </td><td>$row[3] </td></tr>";
}
if($row[4]!=NULL){
print "<tr><td colspan=\"6\">Housing: </td><td>$row[4] </td></tr>";
}
print "</table>";
}
}
print "</table>";
?>
</div>
<?php include '../footer.php';?>
</div>
</body>
</html>
至少一个问题可能是从此处嵌套<table>元素的:
if($row[4]!=NULL || $row[3]!=NULL){
print "<table>";
if($row[3]!=NULL){
从MDN表格元素Doc :
允许的内容按以下顺序:
- 可选的
<caption>
元素,- 零个或多个
<colgroup>
元素,- 可选的
<thead>
元素,- 以下任一项:
- 零个或多个
<tbody>
元素- 一个或多个
<tr>
元素- 可选的
<tfoot>
元素
您可能想要使用诸如Nu Html之类的东西来验证HTML。 一种使用方式是
text input
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.