0

I'm trying to make a page that fetches information from a database and posts it to a webpage. I tried using the "table border" and "table-height"command, but I think Ive done it wrong. Because whenever I echo something now, it shows up above the table, even though the code is underneath the "table" code. Take a look at my code (You shouldnt care about the variables I set as they may not match each other. Do not worry about this. The only thing I want to fix is the table)

<?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']  . "; 
}



?>
tomSurge
  • 256
  • 1
  • 5
  • 15
  • `h1` is not a valid element to be directly inside a `table` tag; you're also missing the `thead`, `tbody`, and `tr` (row) for the `th`. What this means is you're leaving the display up to the browser to comprehend, and it's moving the `h1` and `h4` out of the `table` "for you" (triggering what's called Quirks Mode). Make your markup valid. W3C has a [markup validator](http://validator.w3.org/#validate_by_input) that can help you find your issues. Use the browser's `View Source` to copy your markup as the browser receives it. – Jared Farrish Mar 14 '15 at 22:34
  • mysql is deprecated and should not be used in new code [see here](http://stackoverflow.com/q/12859942/2670892) – greg-449 Mar 15 '15 at 08:55

2 Answers2

1

Try to change this line:

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

with this:

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

Then don't forget to close the at the end, but I think also there is some problem with the because it's not in any table TAG

nik.longstone
  • 244
  • 2
  • 8
0

I add the lines that you need in yout code see the comments "// LINE ADDED". Try with this code:

<?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 

?>

I think that you should read this:

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

Adrian Cid Almaguer
  • 7,815
  • 13
  • 41
  • 63