php while循环回显循环内容之外的元素

The forum pages on my website use PHP to create a table and then use a while loop to populate it from the database. This works fine and always has but I have tried to move the anchor, 'link', tag from around the post's title to the entire first section of the post within the table. To do this it goes through the following steps:

  1. Open the table tag [OUTSIDE OF LOOP]
  2. Echo headers [OUTSIDE OF LOOP]
  3. Start WHILE loop that makes another post section for every post found.
  4. Create table row
  5. Create table data
  6. Echo content
  7. Close table data
  8. REPEAT STEPS 5-7 ONCE MORE for post date section
  9. Close table row
  10. close table [OUSTIDE OF LOOP]

It should make the links clickable on all of the first section and they should be within the table like this:

<table>  <--- *THIS IS BEFORE THE LOOP, IT GETS RUN ONCE ONLY* -->
    <WHILE *do this like 5 times or something*>
      <tr>
        <a *category link*>
          <td>
            *content for the 'td' which is taken from the DB*
          </td>
          <td>
            *content for the 'td' which is taken from the DB*
          </td>
        </a>
      </tr>
      <ENDWHILE>
</table>

However, in practice they end up outside of the table as can be seen in this screenshot:

preview showing anchors outside of table.

Could anyone please explain this and how to fix it?

echo '<table class="forumTable">
  <tr>
  <th>Category</th>
  <th>Last topic</th>
  </tr>';

while($row = mysqli_fetch_assoc($catResult)){
  echo '<tr>';
  echo '<a href="category.php?id=' . htmlspecialchars($row['catID']) . '"><td class="catDesc">';
  echo '<h3>' . $row['catName'] . '</h3>' . $row['catDesc'];
  echo '</td>';
  echo '<td class="catTime">';
  $getTops = "SELECT topicID, topicSubject, topicDate, topicCat FROM topics WHERE topicCat = " . $row['catID'] . " ORDER BY topicDate DESC LIMIT 1";
  $topResult = mysqli_query($connect, $getTops);
  if(!$topResult){
    echo '<p style="margin-top: 75px;">The last topic could not be displayed, please try again later.</p>';
  }
  else{
    if(mysqli_num_rows($topResult) == 0){
      echo '<p>No topics</p>';
    }
    else{
      while($topRow = mysqli_fetch_assoc($topResult)){
        echo '<a href="topic.php?id=' . $topRow['topicID'] . '">' . $topRow['topicSubject'] . '</a> at ' . $topRow['topicDate'];
      }
    }
  }
  echo '</td></a>';
  echo '</tr>';
}
echo '</table>';

Since the source page confirms that the anchors are where you placed them, but the browser moves them around, you can either : - contain your links inside the td table cell - use an alternative approach to add the link where you want it html - table row like a link

Did you try to get page not from browser? How it looks? I think browser does not allow you to put <a> into <table> directly without <tr><td> </td></tr>