0

PHPを使ってデータベースからテーブル形式でデータを出力したい。& 表に別の色を表示

しかし、それは代替色を表示しません。ブラウザはphpコードが開始する前にdivタグを作成しているため、色は表示されません。

コードの一部:

$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>";
4

3 に答える 3

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>";
于 2013-05-28T20:15:33.717 に答える
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>";
于 2013-05-28T20:14:52.150 に答える
0

div タグを tr タグの外に置くべきではありません。class 属性を tr タグに設定することをお勧めします。

<tr class="odd">
于 2013-05-28T20:15:15.437 に答える