简体   繁体   English

从MySQL数据库填充html表时出错。

[英]Error populating html table from MySQL database.

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. 我正在尝试使用数据库中的数据填充html表,但是每增加一行新表,该表就会向下移动。 If for example, 38 rows is added to the table, the table becomes invisible. 例如,如果将38行添加到表中,该表将变为不可见。 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>

";

You cannot have <th> directly inside <table> . 您不能直接在<table>包含<th> <table> Please wrap them inside <tr> . 请将它们包装在<tr>

You must get the rows from the foreach in order to get the result rows. 您必须从foreach中获取行才能获取结果行。 The one that you are looping is just the stdObject ResultSet . 您要循环播放的是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> . 是的,正如其他人所说, <table>标记只能包含<tbody><thead><tfoot><tr> Nothing else. 没有其他的。 There's no tag like </br> . 没有像</br>这样的标签。 It must be <br /> . 它必须是<br /> Brush up the basics of HTML. 复习HTML的基础知识。

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. 就目前而言,代码将生成无效的表标记,因此表的样式和渲染将有些不确定,并且每种浏览器可能会有所不同。

You need to wrap you <th></th> in <tr></tr> too, 您还需要将<th></th>包裹在<tr></tr>

That's indeed the case. 确实是这样。

Second of all, I will advice not using echo in this case. 第二,我建议在这种情况下不要使用echo 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. 这将是我想要的代码,因为您会将HTML颜色保留在代码编辑器中,这使得其他人更容易调试。

Little explanation: <?= and ?> are easy to use open and close tags to echo a variable within HTML. 很少解释: <?=?>易于使用打开和关闭标签来回显HTML中的变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM