user1261817
user1261817

Reputation: 1593

PHP/MySQL: Footer displayed before table (powered by a database)

On my page, I have a table (content from MySQL) and a footer (using require_once()).

The problem is, even though the "require_once("footer.php");" line of code is after my table display code, the footer is displayed above my table.

Here's my code:

$table='mytable';
$result = mysql_query("SELECT * FROM {$table}");
if (!$result) {
    die("Query to show fields from table failed");
}

$fields_num = mysql_num_fields($result);

//echo "<h1>Table: {$table}</h1>";
echo "<table border='1'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
    $field = mysql_fetch_field($result);
    echo "<td>{$field->name}</td>";
}
echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
    echo "<tr>";

    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    foreach($row as $cell)
        echo "<td>$cell</td>";

    echo "</tr>\n";
}
mysql_free_result($result);
?>

<?
    require_once("footer.php"); 
?>

Thanks!

Upvotes: 2

Views: 2005

Answers (4)

Chirag
Chirag

Reputation: 1128

Where is your closing tag for table?

Put this

echo "</table>";

Upvotes: 1

Nathan
Nathan

Reputation: 4067

Make sure you are closing the </table> tag.

Also, this may be an issue with CSS.

Upvotes: 1

JScoobyCed
JScoobyCed

Reputation: 10413

I think you need to close the table tag:

 mysql_free_result($result);
 echo "</table>"
 require_once("footer.php"); 
?>

Upvotes: 1

Vincent Ramdhanie
Vincent Ramdhanie

Reputation: 103135

You are not closing the table tag after the loop constructing the table.

  echo "</table>";

Place that after the while loop.

Upvotes: 5

Related Questions