php - 如何在不破坏我的 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>
解决方案
你可以这样做:
<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>
希望它会帮助你。
推荐阅读
- mariadb - MariaDB如何在现有数据库中进行字符集转换?
- node.js - 使用 ngrx 部分同步客户端状态
- c# - Windows 服务可以使用由另一个 Windows 服务托管的 WCF 服务吗?
- java - 无法实例化类型(类)?
- angular - 如何避免 Angular 中重复的 ViewChildren?
- perl - perl 中的 getopts 问题
- javascript - javascript中ceil函数的问题
- list - 对列表飞镖内的整数列表求和
- gradle - 无法解析符号junit
- botframework - 无法设置 OAuthconnection 设置