如何从mysql表中获取行并将其填入html表中

时间:2013-08-21 23:24:20

标签: html html-table

我是PHP和mysql的新手,对不起,如果这是一个新手的错误。

我很难从mysql中获取数据并将其填入html表中。

所以我的脚本如下:

<html>
<head>

</head>
<body>
    <div class="well">
        <table class="table table-striped">
            <?php
            $userDetail = mysql_query("SELECT * FROM uyePopup ORDER BY id LIMIT 100") or die(mysql_error());
            $useridTable = "";
            $userGenderTable = "";
            $userMailTable = "";
            while ($userRow = mysql_fetch_array($userDetail)) {

                $useridTable.= '<td>' . $userRow['id'] . '</td>';
                if ($userRow['gender'] == 1) {
                    $userRow['gender'] = 'M';
                } else {
                    $userRow['gender'] = 'F';
                }
                $userGenderTable.= '<td>' . $userRow['gender'] . '</td>';
                $userMailTable.= '<td>' . $userRow['email'] . '</td>';
            }
            // echo $userTable;
            ?>
            <thead>
            <tr>
                <th>ID</th>
                <th>Gender</th>
                <th>E-mail</th>
            </tr>
            </thead>
            <tbody>
            <tr>
                <?= $useridTable ?>
            </tr>
            <tr>
                <?= $userGenderTable ?>
            </tr>
            <tr>
                <?= $userMailTable ?>
            </tr>
            </tbody>
        </table>
    </div>
</body>
</html>

所以我有这样的输出:

我用Google搜索了很长时间,但似乎没什么用。

1 个答案:

答案 0 :(得分:1)

这是未经测试的,但这样的事情应该有效:

<html>
<head>

</head>
<body>
    <div class="well">
        <table class="table table-striped">
            <thead>
            <tr>
                <th>ID</th>
                <th>Gender</th>
                <th>E-mail</th>
            </tr>
            </thead>
            <tbody>
            <?php
            $userDetail = mysql_query("SELECT * FROM uyePopup ORDER BY id LIMIT 100") or die(mysql_error());
            while ($userRow = mysql_fetch_array($userDetail)) {
               echo '<tr><td>' . $userRow['id'] . '</td>';
               echo '<td>'.($userRow['gender'] == 1 ? 'M' : 'F').'</td>';
               echo '<td>' . $userRow['email'] . "</td></tr>\n";
            }
            ?>
            </tbody>
        </table>
    </div>
</body>
</html>