php while 循环回显循环内容之外的元素
php while loop echoing element outside of looped content
我网站上的论坛页面使用 PHP 创建一个 table,然后使用 while 循环从数据库中填充它。这工作正常而且一直如此,但我试图将锚 'link' 标记从 post 的标题周围移动到 [=54] 中 post 的整个第一部分=].为此,它需要执行以下步骤:
- 打开 table 标签 [循环外]
- Echo headers [循环外]
- 启动 WHILE 循环,为找到的每个 post 创建另一个 post 部分。
- 创建 table 行
- 创建table数据
- 回显内容
- 关闭table数据
- post 日期部分再重复步骤 5-7 一次
- 关闭 table 行
- 关闭 table [循环外]
它应该使所有第一部分的链接都可以点击,并且它们应该在 table 内,如下所示:
<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>
然而,实际上它们最终在 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>';
您是否尝试从浏览器获取页面?它看起来怎么样?
我认为浏览器不允许您在没有 <tr><td> </td></tr>
的情况下直接将 <a>
放入 <table>
由于源页面确认锚点位于您放置它们的位置,但浏览器移动了它们,您可以:
- 将你的 links 包含在 td table 单元格中
- 使用替代方法在您想要的地方添加 link html - table row like a link
我网站上的论坛页面使用 PHP 创建一个 table,然后使用 while 循环从数据库中填充它。这工作正常而且一直如此,但我试图将锚 'link' 标记从 post 的标题周围移动到 [=54] 中 post 的整个第一部分=].为此,它需要执行以下步骤:
- 打开 table 标签 [循环外]
- Echo headers [循环外]
- 启动 WHILE 循环,为找到的每个 post 创建另一个 post 部分。
- 创建 table 行
- 创建table数据
- 回显内容
- 关闭table数据
- post 日期部分再重复步骤 5-7 一次
- 关闭 table 行
- 关闭 table [循环外]
它应该使所有第一部分的链接都可以点击,并且它们应该在 table 内,如下所示:
<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>
然而,实际上它们最终在 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>';
您是否尝试从浏览器获取页面?它看起来怎么样?
我认为浏览器不允许您在没有 <tr><td> </td></tr>
<a>
放入 <table>
由于源页面确认锚点位于您放置它们的位置,但浏览器移动了它们,您可以: - 将你的 links 包含在 td table 单元格中 - 使用替代方法在您想要的地方添加 link html - table row like a link