PHP 回显表,将其附加到 DOM 中的特定元素


PHP echoing a table, appending it to a specific element in the DOM?

我有这段代码,它检查一个数据库并向我的PHP代码返回一些行,其中包含4个值(id,playerA,playerB,turn,all INT)。

我想使用该数组来构建一个表,然后将该表附加到 DOM 中的特定位置,但我不知道该怎么做。

我可以用另一种方式做到这一点(通过 JS Ajax 获取行,然后使用 JS 构建和附加表),我知道怎么做,但我不想那样做。

是否可以创建一个表并使用php/html/css将其附加到div?

谢谢

<?php
if (isset($_SESSION["userid"])){
    $dbManager = DBManager::app();
    $manager = new Manager($_SESSION["userid"]);
    $gameList = $manager->getGames();
    if ($gameList) {
        Debug::log("got active games: ".sizeof($gameList);
    }
    else {
        Debug::log("no games");
    }   
}
else {
        Debug::log("no user id");
}
?>
<!DOCTYPE html>
<html>
<head>
    <link rel='stylesheet' href='style.css'/>
    <script src="jquery-2.1.1.min.js"></script>
    <script src='script.js'></script>
    <script src='ajax.js'></script>
</head>
    <body>
        <input type="button" value="createGame" onclick="createGame()">
        <divid="gameListDiv">
        <div><a href="logout.php">LOGOUT</a></div>
    </body>
</html>

编辑

 <?php
    $table = "";
    if ($gameList) {
        foreach ($gameList as $game){       
            $table += "<tr>";
            $table += "<td>";
            $table += $game["name"];
            $table += "</td>";
            $table += "</tr>";
        }
        $table += "</table>";
    }
?>
    <body>
        <input type="form" id="gameName" placeholder="Enter Game Name here"></input>    
        <input type="button" value="createGame" onclick="createGame()"></input>
        <div>
            <span>Active Games</span>
            <?php echo $table; ?>       
        </div>              
        <div><a href="logout.php">LOGOUT</a></div>
    </body>

你需要明白 DOM 还不存在 - 它是由浏览器创建的,浏览器根据组合的 PHP 和 HTML 的输出构建它。

有很多方法可以解决这个问题,而无需诉诸 Ajax 调用等。

<!DOCTYPE html>
<html>
<head>
    <link rel='stylesheet' href='style.css'/>
    <script src="jquery-2.1.1.min.js"></script>
    <script src='script.js'></script>
    <script src='ajax.js'></script>
</head>
    <body>
        <input type="button" value="createGame" onclick="createGame()">
        <div id="gameListDiv">
        <?php
            if (isset($_SESSION["userid"])){
                $dbManager = DBManager::app();
                $manager = new Manager($_SESSION["userid"]);
                $gameList = $manager->getGames();
                if ($gameList) {
                    Debug::log("got active games: ".sizeof($gameList);
                    echo '<table style="width:100%">';
                    //assuming we can iterate over the $gameList value
                    foreach($gameList as &$game)
                    {
                        //here i assume that the result returned is an object with these properties - it might be the case that you need to do something like $game['playerA'] or $game->getData('playerA') - i am not sure what database lib you are using.
                        echo '<tr>
                            <td>'.$game->playerA.'</td>
                            <td>'.$game->playerB.'</td>
                            <td>'.$game->turn.'</td>
                          </tr>';
                    }
                    echo '</table>';
                }
                else {
                    Debug::log("no games");
                }   
            }
            else {
                    Debug::log("no user id");
            }
            ?>
        </div>
        <div><a href="logout.php">LOGOUT</a></div>
    </body>
</html>

在这个例子中,我们只是在HTML中运行PHP。

如果你想把所有的数据库逻辑都保留在页面的顶部,而不是与HTML内联,你也可以做这样的事情:

<?php
    /* Database logic here */
    $variable = '<span>this variable could contain any old html that came from the database logic</span>';
?>
<html>
<head>
</head>
<body>
    <php echo $variable; ?>
</body>
</html>
如果你想在

页面加载之前构建列表,你可以在你想要表格的位置插入这样的东西:

<table>
    <?php foreach($gamelist as $game){ ?>
    <tr>
        <td><?=$game.id></td>
        <td><?=$game.playerA></td>
        <td><?=$game.playerB></td>
        <td><?=$game.turn></td>
    </tr>
    <? } ?>
</table>

这将起作用,因为使用 PHP,当您将数据放入$gamelist时,页面尚未构建并显示给用户。在页面实际发送给用户之前,您的所有PHP代码都将运行 - 这意味着您以后不需要"更新"页面,您可以立即构建它。

如果您希望在页面加载后收集或更新数据(从您的评论中不清楚),这仅使用 PHP 是不可能的,因为它是一种服务器端语言。您需要重新加载页面或使用 AJAX 来实现此目的。