自动从数据库中获取数据并显示在html上[已关闭]

dldeef67  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(81)

**已关闭。**此问题需要debugging details。目前不接受回答。

编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答问题。
8天前关闭
Improve this question
我有一个Post表,列为post_iduser_idcontentpost_datelikescomments
这里是index.php

<?php
if ($select_post->num_rows > 0) {
    while ($row = $select_post->fetch_assoc()) {
        $content = $row["content"];
        $user = $row["username"];
        $likes = $row["likes"];
        $cmt = $row["cmt_content"];
    }
} else {
    echo "Nothing!.";
}
?>
<div class="grid-container" id="gridContainer">
</div>

字符串
添加一个新的div标签到index.php的函数

function addNewPost() {
    var title = document.getElementById('postTitle').value;
    var content = document.getElementById('postContent').value;

  
    var newPost = document.createElement("div");
    newPost.className = "grid-item";

    
    newPost.innerHTML = `
    <div class="grid-item">
        <img src="https://placekitten.com/300/200" alt="Post Image">
        <div class="content">
            <p><?php echo $content ?></p>
        </div>
        <div class="post-meta">
            <span class="like-count"><?php echo $likes ?></span>
            <button class="interaction-btn" onclick="likePost(this)"><i class="fas fa-heart"></i></button>
        </div>
    `;

    document.getElementById("gridContainer").appendChild(newPost);

}


我想当我插入一个新的列表和重新加载页面的新职位将在那里。但没有数据显示

0yycz8jy

0yycz8jy1#

简单的方法是这样的:你不需要js,直接在 *while循环 * 中使用你的data

<div class="grid-container" id="gridContainer">
<?php
if ($select_post->num_rows > 0) {
    while ($row = $select_post->fetch_assoc()) {
        $content = $row["content"];
        $user = $row["username"];
        $likes = $row["likes"];
        $cmt = $row["cmt_content"];
?>
    <div class="grid-item">
        <img src="https://placekitten.com/300/200" alt="Post Image">
        <div class="content">
            <p><?php echo $content ?></p>
        </div>
        <div class="post-meta">
            <span class="like-count"><?php echo $likes ?></span>
            <button class="interaction-btn" onclick="likePost(this)"><i class="fas fa-heart"></i></button>
        </div>
<?php
    }
} else {
    echo "Nothing!.";
}
?>
</div>

字符串

相关问题