如何消除用PHP创建的表顶部的额外换行符?

如何消除用PHP创建的表顶部的额外换行符?,php,css,html,mysqli,html-table,Php,Css,Html,Mysqli,Html Table,我刚刚开始用PHP编程。我遇到了一个问题,我无法用我在网上找到的任何解决方案来解决 我试图显示一个带有论坛上创建的线程的表。我最初创建了一个静态html代码来查看表的结果。一切都很好。问题是当我创建一个动态表时。我的意思是,使用PHP语言用MySQL数据库中的信息更新表 当我用PHP创建表时,表显示为向下的一半(表上方的空白)。我检查了我在网上找到的不同解决方案,但目前我仍然有这个问题。在查看Chrome上的页面时,我发现有几十个br,我不完全了解它们的来源。在我编写的代码中,该区域没有任何br

我刚刚开始用PHP编程。我遇到了一个问题,我无法用我在网上找到的任何解决方案来解决

我试图显示一个带有论坛上创建的线程的表。我最初创建了一个静态html代码来查看表的结果。一切都很好。问题是当我创建一个动态表时。我的意思是,使用PHP语言用MySQL数据库中的信息更新表

当我用PHP创建表时,表显示为向下的一半(表上方的空白)。我检查了我在网上找到的不同解决方案,但目前我仍然有这个问题。在查看Chrome上的页面时,我发现有几十个br,我不完全了解它们的来源。在我编写的代码中,该区域没有任何br,但浏览器正在解释导致添加大量br(确切地说是45)的内容。我尝试了Firefox,同样的情况也发生了

我读过关于从代码中删除大小写或空格的内容。将边距和填充设置为零(显然没有效果)。我不知道是什么导致了额外的br

这是密码。我正在使用html5、css3、PHP版本。7.1.11和MySQL

<?php
include 'connection.php';
session_start();
$user=$_SESSION['logged_username'];
$unread_messages = $connection -> query ("SELECT unread_messages FROM userlist WHERE username ='$user'") or die ("Connection has failed.");

echo "This is the homepage." . '<br/>' . '<br/>';
echo "Session started as: " . 
     '<b>' . $_SESSION['logged_username'] . '</b>' . 
     " (" . 
     '<a href="logout.php">Cerrar sesión</a>' 
     . ")" . '<br/>';

//shows the number of unread messages
while($variable = $unread_messages->fetch_assoc())
{$_SESSION['message_amount'] = $variable['unread_messages'];}
?>

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<p style="display: inline-block;">Unread messages: </p><a style="display: inline-block;" href="mensajes.php"><?php echo $_SESSION['message_amount']; ?>
</a>
<a href=""></a>

<br><br>
<a href="post_thread.php" style="text-decoration: none; background-color: green; padding: 5px 10px; color: white;">Create new thread</a>

<hr>
<center><div>Lastest published threads</div></center>
<?php 
echo '<table style="width:100%; margin:0; padding:0; top:0; valign:top; border-collapse:collapse;">';
    echo '<tr>';
        echo '<th align="center" width="5%" bgcolor="#f5f5f5">Hour</th>';
        echo '<th align="center" width="85%" bgcolor="#f5f5f5">Thread title</th>';
        echo '<th align="center" width="10%" bgcolor="#f5f5f5">Author</th>';
    echo '</tr>';

    //obtain the highest index from column 'id_thread' (highest = more recent thread).
    $idthreat_result=mysqli_query($connection, "SELECT MAX(id_thread) FROM threads");
    $row=mysqli_fetch_array($idthreat_result);

    $mostrecent_thread = $row[0]; //stores the id of the most recent thread

    //shows in first place the most recent thread
    for ($i=$mostrecent_thread; $i > 0 ; $i--) 
    {   
        //obtains information from database
        //HOUR
        $result_creationhour=mysqli_query($connection, "SELECT creation_hour FROM threads WHERE id_thread='$i'");
        $row_hour=mysqli_fetch_array($result_creationhour);
        //THREAD'S TITLE
        $result_threadtitle=mysqli_query($connection, "SELECT title FROM threads WHERE id_thread='$i'");
        $row_title=mysqli_fetch_array($result_threadtitle);
        //THREAD'S AUTHOR
        $result_threadauthor=mysqli_query($connection, "SELECT creator FROM threads WHERE id_thread='$i'");
        $row_author=mysqli_fetch_array($result_threadauthor);


        //shows the information throught the browser using <tr> + <td>
        //each tr correspond to a row
        echo '<tr>';
            echo '<td>' . $row_hour[0] . '</td>' . '<br/>'; //hour when the thread was created
            echo '<td>' . $row_title[0] . '</td>' . '<br/>'; //title of the thread
            echo '<td>' . $row_author[0] . '</td>' . '<br/>'; //author of the thread
        echo '</tr>';
    }
echo '</table>';
?>
</body>
</html>

只需删除本部分代码中
之后的

    echo '<tr>';
        echo '<td>' . $row_hour[0] . '</td>' ; //hour when the thread was created
        echo '<td>' . $row_title[0] . '</td>' ; //title of the thread
        echo '<td>' . $row_author[0] . '</td>' ; //author of the thread
    echo '</tr>';
echo';
回显“”$行每小时[0]。'//创建线程时的小时数
回显“”$行标题[0]。'//线程的标题
回显“”$行作者[0]。'//线程的作者
回声';

因为

结构中不可用,浏览器会在前面显示它们。

只需在这部分代码的
之后删除

    echo '<tr>';
        echo '<td>' . $row_hour[0] . '</td>' ; //hour when the thread was created
        echo '<td>' . $row_title[0] . '</td>' ; //title of the thread
        echo '<td>' . $row_author[0] . '</td>' ; //author of the thread
    echo '</tr>';
echo';
回显“”$行每小时[0]。'//创建线程时的小时数
回显“”$行标题[0]。'//线程的标题
回显“”$行作者[0]。'//线程的作者
回声';

因为

结构中不可用,并且浏览器之前显示了它们。

即将发布该答案。正是这个[+1]太完美了。非常感谢你。它起到了预期的作用。读了你的答案后,我不明白为什么我在那里使用了br标签。只要删除它们,空白就会自动消失。再次感谢。我正要发布那个答案。正是这个[+1]太完美了。非常感谢你。它起到了预期的作用。读了你的答案后,我不明白为什么我在那里使用了br标签。只要删除它们,空白就会自动消失。再次感谢。只有
th
td
元素应该是
tr
s的子元素。只有
th
td
元素应该是
tr
s的子元素。