1

我已经使用 PHP 从 MYSQL 数据库构建了一个表。有 4 列检查它们是否包含yes在其中,只有在存在时才显示。唯一的问题是,当第一列repair没有任何内容,但后续列有yes时,它们会将输出显示yesrepair列中,而不是在那里留下空白点并将其放在正确的列下。列标题显示正确,它只是让数据出现在正确的列中。不知道如何去做,所以任何帮助表示赞赏。

//build table for results
echo "<table><tr>";
echo "<th>Part Number</th>";
echo "<th>Serial Number</th>";
echo "<th>Date</th>";

//check whether each element has data
//and only display is data present
if ($repair=="yes") echo "<th>Repair</th>";
if ($upgrade=="yes") echo "<th>Upgrade</th>";
if ($pm=="yes") echo "<th>PM</th>";
if ($nofault=="yes") echo "<th>No Fault</th></tr>";

// printing table rows
while($row = mysql_fetch_array($result))
{
$part=$row['part'];
$serial=$row['serial'];
$date=$row['date'];
$Repair=$row['repair'];
$Upgrade=$row['upgrade'];
$PM=$row['pm'];
$NoFault=$row['nofault'];

echo "<tr>";
echo "<td>$part</td>";
echo "<td>$serial</td>";
echo "<td>$date</td>";
if ($Repair=="yes") echo "<td>YES</td>";
if ($Upgrade=="yes") echo "<td>YES</td>";
if ($PM=="yes") echo "<td>YES</td>";
if ($NoFault=="yes") echo "<td>YES</td>";
echo "</tr>";
}

echo "</table>";
4

3 回答 3

1

当 if 评估为 false 时,您应该包含空单元格。像这样的东西:

if ($Repair == "yes") {
 echo "<td>yes</td>"
} else {
 echo "<td></td>"
}

在这种情况下,您也可以直接在 td 中回显该值。像这样的东西:

echo "<td>";
if ($Repair == "yes") echo $Repair;
echo "</td>";
于 2012-08-05T01:53:41.887 回答
1

这种逻辑可能会有所帮助

if ($repair=="yes") echo "<th>Repair</th>" else echo "<th>&nbsp;</th>";

相同的代码

if ($Repair=="yes") echo "<td>YES</td>" else echo "<td>&nbsp;</td>";
于 2012-08-05T01:54:20.250 回答
1

您需要以<td></td>任何一种方式输出。您使用的代码风格非常混乱(大量带有双引号 HTML 字符串的 echo 语句)。这是一个更好的方法:

//build table for results

?>

<table>

<tr>
<th>Part Number</th>
<th>Serial Number</th>
<th>Date</th>

<?php

//check whether each element has data
//and only display is data present
if ($repair=="yes") echo "<th>Repair</th>";
if ($upgrade=="yes") echo "<th>Upgrade</th>";
if ($pm=="yes") echo "<th>PM</th>";
if ($nofault=="yes") echo "<th>No Fault</th></tr>";

// printing table rows
while( $row = mysql_fetch_array( $result ) ) {

?>

<tr>

<td><?php echo $part; ?></td>

<td><?php echo $serial; ?></td>

<td><?php echo $date; ?></td>

<td><?php echo $row[ 'Repair' ] == "yes" ? "YES" : ""; ?></td>

<td><?php echo $row[ 'Upgrade' ] == "yes" ? "YES" : ""; ?></td>

<td><?php echo $row[ 'PM' ] == "yes" ? "YES" : ""; ?></td>

<td><?php echo $row[ 'NoFault' ] == "yes" ? "YES" : ""; ?></td>

</tr>

<?php

}
// while

?>

</table>
于 2012-08-05T01:57:37.620 回答