2017-02-16 144 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