0

I am trying to populate my html table using data from my database but each a new row is added to the table, the table shifts downwards. If for example, 38 rows is added to the table, the table becomes invisible. You have to scroll down to see the table. What might the problem be?

echo "
<div class='container'>

        <span class='cc' style='font-weight: bold;'>Count: $stat</span></br></br>
<table class='col-md-9' border='1'>

    <th>Agent's name</th>
    <th>Student's name</th>
    <th>Student's number</th>
    <th>Accommodation type</th>
    <th>School</th>
    <th>Date & Time</th>

    ";
foreach($db->query($select) AS $result){

    echo "

    <tr><td>{$result['agent_name']}</td>
    <td>{$result['student_name']}</td>
    <td>{$result['student_number']}</td>
        <td>{$result['accommodation']}</td>
    <td>{$result['school']}</td>
    <td>{$result['contact_date']}</td>
    </tr>
    </br>

    ";   
}
echo "

</table>
</div>

";
2
  • You need to wrap your <th></th> in <tr></tr> too, Commented Oct 8, 2015 at 12:22
  • what errors are occur Commented Oct 8, 2015 at 12:23

3 Answers 3

3

You cannot have <th> directly inside <table>. Please wrap them inside <tr>.

You must get the rows from the foreach in order to get the result rows. The one that you are looping is just the stdObject ResultSet. Change your code to:

$result = $db->query($select)
if (mysqli_num_rows($result) > 0)
  while (false != ($row = mysqli_fetch_array($result)))
    // Loop here.

Yes, and as others said, a <table> tag can contain only <tbody>, <thead>, <tfoot> and <tr>. Nothing else. There's no tag like </br>. It must be <br />. Brush up the basics of HTML.

Sign up to request clarification or add additional context in comments.

2 Comments

But what about <td></td> Pretty sure a table can contain those as well.
@Epodax They cannot come directly as a child to <table>.
0

Two things:

First, you need to wrap your header elements in a row:

<tr> <!-- here -->
<th>Agent's name</th>
<th>Student's name</th>
<th>Student's number</th>
<th>Accommodation type</th>
<th>School</th>
<th>Date & Time</th>
</tr> <!-- and here -->

Second, what on Earth are these?:

</br>

The browser is probably interpreting them as this:

<br />

Which means you're adding a line break inside your table structure with each row. Which is probably invalid (since it's outside the table cell and part of the table itself), but probably also why each row pushes the display down a little further. Remove that from your loop.

As it stands the code is generating invalid table markup, so styling and rendering the table is going to be somewhat undefined and possibly different for each browser.

4 Comments

Er... That would be a she! :P
@PraveenKumar: I corrected it. Though that's also assuming the photo is accurate. Wouldn't be the first time it wasn't :)
LoL, @David. Could be.
@radioactive Did you look at my answer?
0

You need to wrap you <th></th> in <tr></tr> too,

That's indeed the case.

Second of all, I will advice not using echo in this case. Here is what the code looks like the way I would do it:

<div class='container'>

<span class='cc' style='font-weight: bold;'><?php Count: $stat ?></span></br></br>
<table class='col-md-9' border='1'>
  <tr>
     <th>Agent's name</th>
     <th>Student's name</th>
     <th>Student's number</th>
     <th>Accommodation type</th>
     <th>School</th>
     <th>Date & Time</th>
 </tr>
 <?php
 foreach($db->query($select) AS $result):
    ?>
    <tr>
        <td><?=$result['agent_name']?></td>
        <td><?=$result['student_name']?></td>
        <td><?=$result['student_number']?></td>
        <td><?=$result['accommodation']?></td>
        <td><?=$result['school']?></td>
        <td><?=$result['contact_date']?></td>
    </tr>
    </br>
<?php
endif;
?>
</table>
</div>

This would be the code I would prefer because you'll keep the HTML colors in your code editor, which makes it easier for others to debug.

Little explanation: <?= and ?> are easy to use open and close tags to echo a variable within HTML.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.