【发布时间】:2017-07-05 20:22:20
【问题描述】:
我网站上的论坛页面使用 PHP 创建一个表,然后使用 while 循环从数据库中填充它。这工作正常并且总是有,但我试图将锚点“链接”标签从帖子标题周围移动到表格中帖子的整个第一部分。为此,它经过以下步骤:
- 打开表格标签 [OUTSIDE OF LOOP]
- 回显标头 [OUTSIDE OF LOOP]
- 启动 WHILE 循环,为找到的每个帖子创建另一个帖子部分。
- 创建表格行
- 创建表数据
- 回声内容
- 关闭表数据
- 对过帐日期部分再重复第 5-7 步
- 关闭表格行
- 关闭表格 [OUSTIDE OF LOOP]
它应该使第一部分的所有链接都可以点击,并且它们应该在表格中,如下所示:
<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>
但是,实际上它们最终会出现在表格之外,如下图所示:
谁能解释一下这个以及如何解决它?
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>';
【问题讨论】:
-
你需要显示实际代码,而不是
<WHILE *do this like 5 times or something*>。 -
显示一些代码以便我们能够回答您的问题。
-
啊,是的,对不起,我忘了这样做。编辑了帖子。
-
你能检查页面源代码(右键单击 >> 显示页面源代码)而不是 chrome 开发工具显示的内容吗?我怀疑浏览器不喜欢链接的位置并将其移动到更合适的位置。该链接应该包含在单元格内。对于跨越多个单元格的内容,请参见此处:stackoverflow.com/questions/1460958/html-table-row-like-a-link
-
您刚刚体验了编写无效 HTML 时发生的情况,并且浏览器纠错机制启动并尝试在创建 DOM 时理解它。因此,始终将验证您的 HTML 代码作为故障排除的第一步。 validator.nu, validator.w3.org
标签: php html loops html-table anchor