<div>类没有显示在php </div>中

时间:2013-05-28 20:12:42

标签: php mysql html

我想使用php从数据库以表格形式输出数据。 &安培;表格中有其他颜色

但它没有显示替代颜色,浏览器在php代码启动之前制作div标签&amp;所以颜色不显示。

部分代码:

$result = mysqli_query($con, "SELECT * FROM form1 ORDER by id   ");

    echo "<table border=1>
        <tr>
        <th>id</th>
        <th>Name</th>
        <th>userName</th>
        <th>e-Mail id</th>";

    while ($row = mysqli_fetch_array($result)) {
        if ($row['id'] % 2 == 1) {

            echo '<div class = "odd">';
            echo "<tr>";
            echo "<td>" . $row['id'] . "</td>";
            echo "<td>" . $row['name'] . "</td>";
            echo "<td>" . $row['username'] . "</td>";
            echo "<td>" . $row['mail'] . "</td>";
            echo "</tr>";
            echo '</div>';
        } else {
            echo '<div class = "even">';
            echo "<tr>";
            echo "<td>" . $row['id'] . "</td>";
            echo "<td>" . $row['name'] . "</td>";
            echo "<td>" . $row['username'] . "</td>";
            echo "<td>" . $row['mail'] . "</td>";
            echo "</tr>";
            echo '</div>';
        }
    }

    echo "</table>";

3 个答案:

答案 0 :(得分:3)

只需将类设置为TR

即可
$result = mysqli_query($con, "SELECT * FROM form1 ORDER by id   ");

echo "<table border=1>
    <tr>
    <th>id</th>
    <th>Name</th>
    <th>userName</th>
    <th>e-Mail id</th>
    </tr>";

while ($row = mysqli_fetch_array($result)) {
        echo "<tr class=\"".(($row['id'] % 2 == 1)?"odd":"even")."\">";
        echo "<td>" . $row['id'] . "</td>";
        echo "<td>" . $row['name'] . "</td>";
        echo "<td>" . $row['username'] . "</td>";
        echo "<td>" . $row['mail'] . "</td>";
        echo "</tr>";
}

echo "</table>";

答案 1 :(得分:0)

请改用:

$result = mysqli_query($con, "SELECT * FROM form1 ORDER by id   ");

echo "<table border=1>
    <tr>
    <th>id</th>
    <th>Name</th>
    <th>userName</th>
    <th>e-Mail id</th>
    </tr>";

$class = 'even';
while ($row = mysqli_fetch_array($result)) {
    $class = $class=='even' ? 'odd' : 'even';

        echo "<tr>";
        echo "<td class="' . $class . '">" . $row['id'] . "</td>";
        echo "<td class="' . $class . '">" . $row['name'] . "</td>";
        echo "<td class="' . $class . '">" . $row['username'] . "</td>";
        echo "<td class="' . $class . '">" . $row['mail'] . "</td>";
        echo "</tr>";
}

echo "</table>";

答案 2 :(得分:0)

你不应该把div标签放在tr标签之外,最好将class属性设置为tr标签,比如

<tr class="odd">