douzhuan4406 2017-02-16 15:57
浏览 76
已采纳

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>';
  • 写回答

2条回答 默认 最新

  • doutuobao4004 2017-02-16 16:36
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解