首页 > 解决方案 > 如何在不破坏我的 html 标签的情况下从 mysql 数据库中获取评论

问题描述

我想给我的评论部分一点风格,这里是没有任何 css 的代码,但我想给它一些风格

<div id="comments">
<?php 
    $sql = "SELECT * FROM comments ORDER BY id LIMIT 2";
    $result = mysqli_query($con, $sql);
    if (mysqli_num_rows($result) > 0) {
        while ($row = mysqli_fetch_assoc($result)) {
            echo "<p>";
            echo $row['author'];
            echo "<br>";
            echo $row['message'];
            echo "<br>";
            echo $row['time'];
            echo "</p>";

        }

    } else {
        echo "there are no comments!";
    }
?>
</div>
<button>More comments</button>

下面是我的 html 部分,在处理 USER、COMMENT 和 TIME 存储在我的数据库中的 php 注释时,我希望显示该部分,这是 html,我如何将上述变量回显到下面的 html 标签中?

<div class="media response-info">
<div class="media-left response-text-left">
<a href="#">
<img class="media-object" src="images/c1.jpg" alt="">
</a>
<h5><a href="#">USER</a></h5>
</div>
<div class="media-body response-text-right">
<p>COMMENT</p>
<ul>
<li>TIME</li>
<li><a href="single.html">Reply</a></li>
</ul>
</div>
</div>

标签: phphtml

解决方案


你可以这样做:

    <div id="comments">
   <?php 
    $sql = "SELECT * FROM comments ORDER BY id LIMIT 2";
    $result = mysqli_query($con, $sql);
    if (mysqli_num_rows($result) > 0) {
        while ($row = mysqli_fetch_assoc($result)) { ?>
            <div class="media response-info">
            <div class="media-left response-text-left">
            <a href="#">
            <img class="media-object" src="images/c1.jpg" alt="">
            </a>
            <h5><a href="#"><?php echo $row['author']; ?></a></h5>
            </div>
            <div class="media-body response-text-right">
            <p><?php  echo $row['message']; ?></p>
            <ul>
            <li><?php echo $row['time']; ?> </li>
            <li><a href="single.html">Reply</a></li>
            </ul>
            </div>
            </div>

<?php   }

    } else {
        echo "there are no comments!";
    }
?>
</div>

希望它会帮助你。


推荐阅读