1

我从数据库中得到结果并试图在页面上显示。但对齐不正确。我可以知道我该如何解决吗?

代码在视图

<?php

        echo "<table align='center'>";

        echo "<tr>";
        echo "<td>  Language ID:  </td>";
        echo "</tr>";

        echo "<tr>";
        echo "<td> Language Name:  </td>";
        echo "</tr>";

        echo "<tr>";
        echo "<td> Date Posted :  </td>";
        echo "</tr>";

        if ($query == true) {
            foreach ($query as $row) {

                echo "<tr>";
                echo "<td>" . $row -> pk_bint_language_id . "</td>";
                echo "<td>" . $row -> vchr_language_name . "</td>";
                echo "<td>" . $row -> bint_language_level . "</td>";
                echo "<td>" . $row -> dat_updated_date . "</td>";

                echo "</tr>";

            }

        }

        echo "</table>";
        ?>

输出:

在此处输入图像描述

4

3 回答 3

1

You want to display four rows within the foreach loop, however you have only three in the header section. From what I can see you are missing Language level /added to the example/

 echo "<table align='center'>
          <tr>
            <td> Language ID:  </td>
            <td> Language Name:  </td>
            <td> Language Level:  </td>
            <td> Date Posted :  </td>
          </tr>";

    if ($query == true) {
        foreach ($query as $row) {

            echo "<tr>";
            echo "<td>" . $row -> pk_bint_language_id . "</td>";
            echo "<td>" . $row -> vchr_language_name . "</td>";
            echo "<td>" . $row -> bint_language_level . "</td>";
            echo "<td>" . $row -> dat_updated_date . "</td>";

            echo "</tr>";

        }

    }

    echo "</table>";
于 2013-04-20T12:51:31.267 回答
0

您应该使用表头th而不是td. 至少这是一种更合适的方式。

<table border="1">
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
</tr>
</table>

您可以在 w3school http://www.w3schools.com/html/html_tables.asp上查看

@bborisovs 的答案是正确的。但是如果你想使用 3 td 而不是 4 你也可以这样做

<td colspan="2"> //meainng this single 'td' with take two 'td' place
于 2013-04-21T01:03:23.497 回答
0

You seem to have 3 <td>s in the first table row (<tr>) with the static "Language ID:" and such, but you have 4 <td>s inside the foreach() loop. It seems like you are missing the "Language level" from the static <td>s.

I would recommend to use the <thead> and <tbody> tags as well.

于 2013-04-20T12:51:39.907 回答