带有水平标题的php mysql表

时间:2014-11-17 14:45:34

标签: php mysql

水平标题会继续显示每一行。我希望标题只显示在顶行。我的代码出了什么问题?提前谢谢。

$sql = $conn->prepare("SELECT * FROM timeSheet, timeSheetUsers WHERE timeSheet.userName=timeSheetUsers.userName AND timeSheet.userName=? ORDER BY startTime ASC");

if ($sql->execute(array($_SESSION['userName']))) {
  while ($row = $sql->fetch()) {
    echo "<table border ='1'>";
         echo 
         "<tr>
            <th>username</th>
            <th>date</th>           
            <th>starTime</th>
            <th>endTime</th>
            <th>total</th>                               
         </tr>
         <tr>
            <td>" . $row['userName'] . "</td>
            <td>" . $row['date'] . "</td>
            <td>" . $row['startTime'] . "</td>
            <td>" . $row['endTime'] . "</td>
            <td>" . $row['total'] . "</td>      
         </tr>";
    echo "</table>";
  }
}

##产生的页面##

enter image description here

2 个答案:

答案 0 :(得分:3)

你已经在循环中为table(th)提供了包含标题的行,更改它并使它们在while循环中运行

$sql = $conn->prepare("SELECT * FROM timeSheet, timeSheetUsers WHERE timeSheet.userName=timeSheetUsers.userName AND timeSheet.userName=? ORDER BY startTime ASC");

if ($sql->execute(array($_SESSION['userName']))) {
echo "<table border ='1'>";
         echo "<tr>
            <th>username</th>
            <th>date</th>           
            <th>starTime</th>
            <th>endTime</th>
            <th>total</th>                               
         </tr>";
  while ($row = $sql->fetch()) {
    echo
        " <tr>
            <td>" . $row['userName'] . "</td>
            <td>" . $row['date'] . "</td>
            <td>" . $row['startTime'] . "</td>
            <td>" . $row['endTime'] . "</td>
            <td>" . $row['total'] . "</td>      
         </tr>";

  }
 echo "</table>";
}

答案 1 :(得分:1)

标题不应该是while循环的一部分,这应该可行

if ($sql->execute(array($_SESSION['userName']))) {
    echo "<table border ='1'>";
     echo 
     "<tr>
        <th>username</th>
        <th>date</th>           
        <th>starTime</th>
        <th>endTime</th>
        <th>total</th>                               
     </tr>";
     while ($row = $sql->fetch()) {
        echo  "<tr>
                 <td>" . $row['userName'] . "</td>
                 <td>" . $row['date'] . "</td>
                 <td>" . $row['startTime'] . "</td>
                 <td>" . $row['endTime'] . "</td>
               <td>" . $row['total'] . "</td>      
              </tr>";
     }
     echo "</table>";
}
相关问题