0

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>';
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • You need to show actual code, not ``. – AbraCadaver Feb 16 '17 at 15:58
  • Show some code for us to be able to answer your question. – Ketan Malhotra Feb 16 '17 at 16:00
  • ahh yes sorry, I forgot to do that. edited the post. – iixCarbonxZz Feb 16 '17 at 16:03
  • can you check the page source (right click >> show page source) and not what chrome dev tools shows you? I suspect the browser doesn't like the placement of the link and moves it to a more suitable place. the link is supposed to be contained inside a cell. For something that spans across multiple cells, see here: http://stackoverflow.com/questions/1460958/html-table-row-like-a-link – Bogdan Feb 16 '17 at 16:12
  • Yes @Bogdan the page source confirms that the anchors are where intended and that chrome has moved them. – iixCarbonxZz Feb 16 '17 at 16:25
  • I am currently trialing the javascript method used on the page you linked but the strings are escaping where I type the location address even after I place \ before my speech marks like this: \" – iixCarbonxZz Feb 16 '17 at 16:37
  • 1
    You just experienced what happens when you write invalid HTML, and the browser error correction mechanism kicks in and tries to make some sense of it while creating the DOM. So always validate your HTML code as a first step of troubleshooting. https://validator.nu/, https://validator.w3.org/ – CBroe Feb 16 '17 at 20:38
  • Possible duplicate of [html - table row like a link](http://stackoverflow.com/questions/1460958/html-table-row-like-a-link) – mplungjan Feb 20 '17 at 15:53

2 Answers2

1

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

Community
  • 1
  • 1
Bogdan
  • 398
  • 2
  • 7
  • I am using the javascript method on the page you linked but have run into another problem with escaping the string within the javascript. the \" still allows the string to escape. – iixCarbonxZz Feb 16 '17 at 16:48
  • I'd need more details, but try this : http://stackoverflow.com/questions/17551867/avoid-to-escape-a-special-characters-in-javascript – Bogdan Feb 16 '17 at 17:05
  • `echo '';` is outputting as this [Imgur.com](http://imgur.com/yLWog9v.png) – iixCarbonxZz Feb 16 '17 at 17:12
  • \\" will output \". So try \" which will output ". – Bogdan Feb 16 '17 at 20:26
  • Or better yet, replace the second set of double quotes with escaped single quotes : https://3v4l.org/PKR9t – Bogdan Feb 16 '17 at 20:30
  • sorry, I was away. Thank you @Bogdan, for the initial fix and your last comment that allowed the code to output correctly. – iixCarbonxZz Feb 16 '17 at 21:26
-1

Did you try to get page not from browser? How it looks? I think browser does not allow you to put <a> into <table> directly without <tr><td> </td></tr>

edwardstock
  • 168
  • 2
  • 10
  • You're right - http://stackoverflow.com/questions/6439649/can-inline-elements-contain-block-elements?noredirect=1&lq=1 - but you're probably being downvoted despite being correct because your answer doesn't have much in the way of details and explanation. – alexanderbird Feb 16 '17 at 20:36
  • Sorry for small explanation, i'm just already faced this problem. Have a nice day :) – edwardstock Feb 17 '17 at 09:52