How to get rid of extra line breaks on top of tabl

2019-03-06 22:04发布

问题:

I am just starting coding in PHP. I am experiencing a problem that I am not able to solve with any solution I found on the net.

I am trying to show a table with the threads that have been created on a forum. I initially created a static html code to see the result of the table. All was ok. The problem is when I create a dynamic table. I mean, using PHP language to update the table with the information in MySQL database.

When I created the table with PHP, the table appears half way down (whitespace above the table). I checked different solutions that I found in the net, but at the moment I still have the problem. While inspecting the page on Chrome, I found that there are dozens of br that I don't fully understand where they come from. In the code I am writting, there are not any br in that area, but the browser is interpreting something that causes to add lots of br (45 to be exact). I tried Firefox and the same happens.

I've read about removing capitalization or whitespaces from the code. Setting margin and padding to zero (obviously nothing worked). I have no idea what is causing the extra br.

Here you have the code. I am using html5, css3, PHP ver. 7.1.11 and 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>

回答1:

Just remove your <br> after the </td> in this part of code :

    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>';

Because <br> is not available in <table> structure, and the browser show them before.