PHP表格格式


PHP Table Format

我已经编写了以下代码,它在使用下拉菜单进行选择时创建了一个表。

echo "<table>";
$result=mysql_query($query);
while($rows=mysql_fetch_array($result)){
echo "<tr>";
echo "<th>Find Name:</th>";
echo "<th>Find Description:</th>";
echo "</tr>";
echo "<tr>";
echo "<td>".$rows['findname']."</td>";
echo "<td>".$rows['finddescription']."</td>";
echo "</tr>";
}
echo "</table>";

我遇到的问题是,对于每个返回的记录,"头"都会重复。实时页面可以在这里找到。我只是想知道是否有人可以看看这个,告诉我哪里出了问题。

为这个非常简单的问题道歉,但我已经看了一段时间了,我就是找不到答案。我认为它只需要一双新的眼睛来看待它。

试试这个,你只需要在循环时得到标题

echo "<table>";
echo "<tr>";
echo "<th>Find Name:</th>";
echo "<th>Find Description:</th>";
echo "</tr>";
$result=mysql_query($query);
while($rows=mysql_fetch_array($result)){
echo "<tr>";
echo "<td>".$rows['findname']."</td>";
echo "<td>".$rows['finddescription']."</td>";
echo "</tr>";
}
echo "</table>";

答案很明显,您在循环中重复头的输出。移动

while($rows=mysql_fetch_array($result)){

在第一次之后

echo "</tr>";

您需要将头放在while循环之外:

echo "<table>";
echo "<tr>";
echo "<th>Find Name:</th>";
echo "<th>Find Description:</th>";
echo "</tr>";
$result = mysql_query($query);
while ($rows = mysql_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . $rows['findname'] . "</td>";
    echo "<td>" . $rows['finddescription'] . "</td>";
    echo "</tr>";
}
echo "</table>";

这应该有效:

echo "<table>";
echo "<tr>";
echo "<th>Find Name:</th>";
echo "<th>Find Description:</th>";
echo "</tr>";
$result=mysql_query($query);
while($rows=mysql_fetch_array($result)){
    echo "<tr>";
    echo "<td>".$rows['findname']."</td>";
    echo "<td>".$rows['finddescription']."</td>";
    echo "</tr>";
    }
echo "</table>";

您的头被重复,因为您正在循环中写入它们,对于查询返回的每一行。您只需要将头移到循环之外,这样在查询返回的行开始打印之前,它们只写一次:

echo "<table>";
echo "<tr>";
echo "<th>Find Name:</th>";
echo "<th>Find Description:</th>";
echo "</tr>";
$result=mysql_query($query);
while($rows=mysql_fetch_array($result)){
  echo "<tr>";
  echo "<td>".$rows['findname']."</td>";
  echo "<td>".$rows['finddescription']."</td>";
  echo "</tr>";
}
echo "</table>";

标头重复,因为它们在while循环中,它应该能很好地工作

echo "<table>";
echo "<tr>";
echo "<th>Find Name:</th>";
echo "<th>Find Description:</th>";
echo "</tr>";
$result=mysql_query($query);
while($rows=mysql_fetch_array($result)){
echo "<tr>";
echo "<td>".$rows['findname']."</td>";
echo "<td>".$rows['finddescription']."</td>";
echo "</tr>";
}
echo "</table>";

更改为:

echo "<tr>";
echo "<th>Find Name:</th>";
echo "<th>Find Description:</th>";
echo "</tr>";
echo "<tr>";
while($rows=mysql_fetch_array($result)){
echo "<td>".$rows['findname']."</td>";
echo "<td>".$rows['finddescription']."</td>";
echo "</tr>";
}
echo "</table>";