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 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号