使用" mysql_fetch_array()创建一个表;"不正常

时间:2015-03-14 22:28:21

标签: php html mysql datatable html-table

我试图创建一个从数据库中提取信息并将其发布到网页的页面。我尝试使用"表格边框"和" table-height"命令,但我认为我做错了。因为每当我现在回应一些东西时,它就显示在表格之上,即使代码位于"表格下面#34;码。看看我的代码(你不应该关心我设置的变量,因为它们可能彼此不匹配。不要担心这个。我唯一要解决的就是表格)

<?php
include 'connection.php';


echo "<table border='2' style='border-collapse: collapse'>";  
echo "<th>ID</th><th>Name</th><th>Special</th><th>Size</th>";


while($elev = mysql_fetch_array($resultstudent)){
    echo "<h4> " . $student['Student']  .  $student['Room']  . " </h4>";
    //echo "<h4> has rom " . $ev['Rom'] . "</h4>";
}

echo "<h1> list of rooms </h1>";

while($rom = mysql_fetch_array($resultroom)){
    echo "<tr><td>"   .$rom['Room ID'] . ""   .$rom['Roomname'] .""  . $rom['Special'] ."</td><td>" . $rom['Size']  . "; 
}



?>

2 个答案:

答案 0 :(得分:1)

尝试更改此行:

echo "<th>ID</th><th>Name</th><th>Special</th><th>Size</th>";

用这个:

echo "<tr><th>ID</th><th>Name</th><th>Special</th><th>Size</th></tr>";

然后不要忘记关闭最后,但我认为还有一些问题,因为它不在任何表TAG

答案 1 :(得分:0)

我在您的代码中添加了您需要的行,请参阅注释&#34; // LINE ADDED&#34;。试试这段代码:

<?php
include 'connection.php';

$queryroom = "SELECT * FROM rom";
$querystudent = "SELECT * FROM elev";
$queryspecial = "SELECT * FROM rom WHERE prosjektor = 1";

$resultroomm = mysql_query($queryRom);
$resultstudent = mysql_query($queryElev);
//$resultspecial = mysql_query($queryRomProsjektor);

echo "<table border='2' style='border-collapse: collapse'>";  
echo "<thead><tr>"; // LINE ADDED
echo "<th>ID</th><th>Name</th><th>Special</th><th>Size</th>";
echo "</tr></thead>"; // LINE ADDED

echo "<tbody>"; // LINE ADDED

while($elev = mysql_fetch_array($resultstudent)){
    echo "<tr><td colspan='4'>"; // LINE ADDED
    echo "<h4> " . $elev['Student'] . " has room nr: " .  $elev['Room']  . " </h4>";
    //echo "<h4> has rom " . $ev['Rom'] . "</h4>";
    echo "</td></tr>"; // LINE ADDED
}

echo "<tr><td colspan='4'>"; // LINE ADDED
echo "<h1> list of rooms </h1>";
echo "</td></tr>"; // LINE ADDED

while($rom = mysql_fetch_array($resultroom)){
    echo "<tr><td>" . $rom['Room ID'] . "</td><td>" . $rom['Roomname'] ."</td><td>"  . $rom['Special'] ."</td><td>" . $rom['Size']  . "</td></tr>"; 
}

echo "</tbody></table>"; // LINE ADDED 

?>

我认为你应该读到这个:

  

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/table