繁体   English   中英

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

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

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

试试这个,您只需要从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>";

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

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

标头重复,因为它们在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 "<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>";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM