【问题标题】:Headers on only first row仅第一行的标题
【发布时间】:2018-02-14 15:40:30
【问题描述】:

我正在为一个我已经从事了相当长一段时间的网站项目构建一个表格视图,并且在昨天晚上取得了一些重大突破。但是,仍然困扰该系统的一个问题是表格视图中每隔一行的标题。无论如何要删除标题的回声吗?我试过这样做,但它总是坏掉:(http://www.scrimfinder.net

<!DOCTYPE html>
<html>
<head>
<link href='style.css?lol=<?php echo time(); ?>' rel='stylesheet' type='text/css'>

</head>
<body>


<?php
include_once("dbConnect.php");
if (isset($_GET["frompost"])) {
 echo "<h3>Thank you for your scrim post! It's been tweeted by <a href='http://twitter.com/scrimfinder'>ScrimFinder</a>.";   
 echo "<br>";
} else {
    
}
echo '<table cellspacing="0" cellpadding="7" width="1">  <tr class="top">  </tr> ';  
$sql = "SELECT * FROM scrims ORDER BY id DESC LIMIT 30;";  
$result = mysqli_query($conn, $sql);  
while ($row = mysqli_fetch_array($result)) {    
echo '        
<th>System</th>
<th>Region</th>
<th>Game</th>
<th>Match Type</th>
<th>Time</th>
<th>Time Zone</th>
<th>Date</th>
<th>Gamertag</th>
<th>Twitter</th>

    <tr>          <td><center>'.$row["system"].'</center></td><td class="grey"><center>'.$row["region"].'</center></td>  <td><center>'.$row["game"].'</center></td>  <td class="grey"><center>'.$row["matchtype"].'</center></td>  <td><center>'.$row["time"].'</center></td>  <td class="grey"><center>'.$row["timezone"].'</center></td>  <td><center>'.$row["date"].'</center></td>  <td class="grey"><center>'.$row["gamertag"].'</center></td>  <td><center><a href="http://www.twitter.com/'.$row['twitter'].'" target="_blank"><input type="submit" value="Message '.$row['twitter'].'" class="button"></a></center></td>          </tr>  ';  } echo '</table>';
?>

</body>
</html>

【问题讨论】:

  • 在循环外给出heading的回显,在循环内只回显rows

标签: php html css mysql


【解决方案1】:

您在循环中包含th,这就是它一次又一次显示的原因。您应该在循环之外回显您的th。像这样的

 <!DOCTYPE html>
 <html>
 <head>
 <link href='style.css?lol=<?php echo time(); ?>' rel='stylesheet' 
 type='text/css'>
 </head>
 <body>
 <?php
 include_once("dbConnect.php");
 if (isset($_GET["frompost"])) {
 echo "<h3>Thank you for your scrim post! It's been tweeted by <a 
 href='http://twitter.com/scrimfinder'>ScrimFinder</a>.";   
 echo "<br>";
 } else {
 }
 echo '<table cellspacing="0" cellpadding="7" width="1"><tr class="top">  
 <th>System</th>
 <th>Region</th>
 <th>Game</th>
 <th>Match Type</th>
 <th>Time</th>
 <th>Time Zone</th>
 <th>Date</th>
 <th>Gamertag</th>
 <th>Twitter</th>
 </tr>';  
 $sql = "SELECT * FROM scrims ORDER BY id DESC LIMIT 30;";  
 $result = mysqli_query($conn, $sql);  
 if(mysqli_num_rows($result)  > 0):
 while ($row = mysqli_fetch_array($result) ) {    
 echo '        
 <tr><td><center>'.$row["system"].'</center></td><td class="grey">
 <center>'.$row["region"].'</center></td>  <td>
 <center>'.$row["game"].'</center></td>  <td class="grey"> 
 <center>'.$row["matchtype"].'</center></td>  <td>
 <center>'.$row["time"].'</center></td>  <td class="grey">
 <center>'.$row["timezone"].'</center></td>  <td>
 <center>'.$row["date"].'</center></td>  <td class="grey">
 <center>'.$row["gamertag"].'</center></td>  <td><center><a 
 href="http://www.twitter.com/'.$row['twitter'].'" target="_blank"><input 
 type="submit" value="Message '.$row['twitter'].'" class="button"></a>
 </center></td>          </tr>  ';  }
 else:
 echo "<tr><td colspan = '100'>NO RECORDS FOUND</td></tr>";
 endif;
 echo '</table>';
 ?>

【讨论】:

  • 如果查询返回空怎么办?我们应该得到一个只有标题可用的空表吗?
  • @NikosGkogkopoulos 我已经编辑了我的答案以检查是否返回空
【解决方案2】:

你只需要复制while循环上面的所有'th'标签,这样它只会渲染一次。

【讨论】:

    【解决方案3】:

    你的桌子应该是什么样子的?

    首先是标题,然后是所有内容行?如果是,只需将回声放在您的 while 循环前面

    对于 TH 来说,重要的是您将它们放入 TR 中,就像普通的 TD 一样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-19
      • 1970-01-01
      • 2015-02-26
      • 1970-01-01
      • 1970-01-01
      • 2021-12-11
      • 2021-06-12
      • 2021-06-13
      相关资源
      最近更新 更多