以下代码应该可以帮助您。只需按照您希望的方式回显 html。
$getnews = mysql_query("SELECT * FROM news ORDER BY id DESC") or die(mysql_error());
while ($row = mysql_fetch_assoc($getnews)) {
$id = $row['id'];
$title = $row['title'];
$body = $row['body'];
$date = $row['date'];
echo "<div class='news-article'>";
echo "<div class=\"title\">$title</div><br>";
echo nl2br($body);
echo "<br><div class=\"date_time\">".time_ago($date)."</div>";
echo "</div>";
echo "<hr>";
}
很多时候,很多这样的回声语句只会混淆你正在尝试做的事情。
$getnews = mysql_query("SELECT * FROM news ORDER BY id DESC") or die(mysql_error());
while ($row = mysql_fetch_assoc($getnews)) {
$id = $row['id'];
$title = $row['title'];
$body = $row['body'];
$date = $row['date'];
?>
<div class='news-article'>
<div class="title"><?php echo $title ?></div><br>
<?php echo nl2br($body); ?>
<br><div class="date_time"><?php echo(time_ago($date)) ?></div>
</div>
<hr>
<?php
}
如果你想完成与无序列表相同的事情,你会这样做。
$getnews = mysql_query("SELECT * FROM news ORDER BY id DESC") or die(mysql_error());
echo "<ul class='article-list'>";
while ($row = mysql_fetch_assoc($getnews)) {
$id = $row['id'];
$title = $row['title'];
$body = $row['body'];
$date = $row['date'];
?>
<li class='news-article'>
<div class="title">$title</div><br>
<?php echo nl2br($body); ?>
<br><div class="date_time"><?php echo(time_ago($date)) ?></div>
</li>
<?php
}
echo "</ul>";