【问题标题】:I need to display table header in my code我需要在我的代码中显示表头
【发布时间】:2019-09-07 15:08:35
【问题描述】:

我制作的代码在表格中的每一行之后显示标题。 我只想要一次标题出现在顶部。有帮助吗?

  print "<table border=1>\n"; 
 while ($row = mysql_fetch_array($result)){ 
  $files_field= $row['filename'];
  $files_show= "Uploads/$files_field";
  $descriptionvalue= $row['title'];
print "<tr>";
print "<th>";
echo "header1";
print "</th>";
print "<th>";
echo "header2";
print "</th>";

print "</tr>";


print "<tr>\n"; 
print "\t<td>\n"; 

echo "<font face=arial size=4/>$descriptionvalue</font>";
print "</td>\n";
print "\t<td>\n"; 

echo "<div align=center><a href='".$files_show."' target='_blank'      title='CLICK TO OPEN'>$files_field</a></div>";
print "</td>\n";
print "</tr>\n"; 
} 
print "</table>\n";

【问题讨论】:

  • 循环在哪里?请提及完整代码...在循环之前写标题&在循环中您必须提及循环数据
  • fyi,来自developer.mozilla.org/en-US/docs/Web/HTML/Element/font 关于&lt;font&gt;:“此功能已过时。尽管它可能在某些浏览器中仍然有效,但不鼓励使用它,因为它可能随时被删除。尽量避免使用它。"
  • 好的,我添加了循环然后如何输入代码,以便表格顶部只有一个标题

标签: php html-table display


【解决方案1】:

这个程序将:

  • 从 db 中获取一行
  • 打印标题
  • 打印数据
  • 获取下一行直到结束

听起来你想要:

  • 打印标题
  •   从数据库中获取一行
  •   打印数据
  •   获取下一行直到最后

【讨论】:

    【解决方案2】:

    我认为这应该足够了:

    <?php
    // print the beginning of the table, and the header
    echo "<table border='1'>";
    
    echo "<tr>";
    
    echo "<th>";
    echo "header1";
    echo "</th>";
    
    echo "<th>";
    echo "header2";
    echo "</th>";
    
    echo "</tr>";
    // than loop through the rows and print the rest of the table
    while ($row = mysql_fetch_array($result)) { 
      $files_field = $row['filename'];
      $files_show  = "Uploads/{$files_field}";
    
      $descriptionvalue = $row['title'];
    
      echo "<tr>\n"; 
      echo "\t<td>\n"; 
    
      echo "<font face=arial size='4'/>$descriptionvalue</font>";
    
      echo "</td>";
      echo "<td>"; 
    
      echo "<div align=center><a href='{$files_show}' target='_blank' title='CLICK TO OPEN'>{$files_field}</a></div>";
      echo "</td>";
      echo "</tr>"; 
    } 
    
    echo "</table>";
    

    一些补充说明/建议:

    • 不要混用 print / echo:它们几乎相同,只会造成混淆
    • 不要尝试使用制表符和换行符“美化”源代码:您只是无缘无故地使输出膨胀,请改用浏览器检查器窗口
    • 忘记mysql驱动程序,使用mysqli:前者已经过时了,以至于从PHP的新版本(从7.0开始)中删除了,因此如果平台更新,您的代码将停止工作; mysqli 变体的使用方式有点不同(主要是一个额外的参数),但结果格式相同,如果与旧版本不同,它的行为也非常相似,所以你不会有重写很多东西

    【讨论】:

      猜你喜欢
      相关资源
      最近更新 更多
      热门标签