2017-02-16 66 views
0

我网站上的论坛页面使用PHP创建表格,然后使用while循环从数据库中填充它。这工作正常,并始终有,但我试图将锚点,'链接',标签从帖子标题左右移动到表格中帖子的整个第一部分。要做到这一点,经过以下步骤:PHP while循环呼应元素以外的循环内容

  1. 打开表标签[循环外]
  2. 回声报头[循环外]
  3. 开始WHILE循环,使得对于每一个岗位另一篇文章部分找到。
  4. 创建表行
  5. 创建表数据
  6. 回声内容
  7. 关闭表数据
  8. 重复步骤5-7再次进行后日期部分
  9. 关闭表行
  10. 关闭表[OUSTIDE OF LOOP]

它应该使链接在所有的第一部分点击, d为表这样中:

<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> 

然而,在实践中,他们最终在表格之外可在该截图中可以看出:

preview showing anchors outside of 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>'; 
+0

您需要显示实际的代码,而不是''。 – AbraCadaver

+0

显示一些代码,以便我们能够回答您的问题。 –

+0

ahh对不起,我忘了这么做。编辑帖子。 – iixCarbonxZz

回答

0

由于源页面证实了锚,你把他们,但浏览器四处移动它们,你可以: - 包含您的链接在td表格单元格内 - 使用其他方法将链接添加到您想要的位置html - table row like a link

+0

我在链接的页面上使用了javascript方法,但遇到了另一个转义javascript内部字符串的问题。 “仍然允许字符串逃脱 – iixCarbonxZz

+0

我需要更多的细节,但试试这个:http://stackoverflow.com/questions/17551867/avoid-to-escape-a-special-characters-in-javascript – Bogdan

+0

'echo'';'输出为[imgur.com ](http://imgur.com/yLWog9v.png) – iixCarbonxZz

-1

你尝试从浏览器中得到页面呢?它看起来如何? 我认为浏览器不允许你把<a><table>的情况下直接<tr><td> </td></tr>

+0

你是对的 - http://stackoverflow.com/questions/6439649/can-inline-elements-contain-block-elements?noredirect=1&lq=1 - 但你可能会被低估尽管是正确的,因为你的答案没有太多细节和解释。 – alexanderbird

+0

对不起,我只是已经遇到了这个问题。祝你有个愉快的日子:) – edwardstock