我正在将博客作为学校项目,但有点卡住了。
该项目的要求之一是使用 smarty,这对我来说是全新的。
我的问题是我想将我数据库中的“博客文章”分配给 smarty 变量。 我的做法是这样的:
<?php
require_once('connect_db.php');
$result = $db->query("SELECT * FROM Innlegg");
while ($row = $result->fetch_assoc())
{print ("<h1>" . $row["forfatter"] . "</h1>");
print ($row["innhold"]);}
?>
现在我只是从“Innlegg”打印出“forfatter”。这是如何使用 smarty 完成的?
最佳答案
尝试阅读 the Smarty FAQ首先..它非常简单
$list=array();
while ($row = $result->fetch_assoc()) {
$list[]=$row;
}
$smarty = new Smarty(); //maybe some configuration ?
$smarty->assign('list', $list);
$smarty->fetch('index.tpl');
并在 smarty index.tpl 模板文件中
{foreach from=$list item=row}
<h1>{$row.forfatter}</h1>
{$row.innhold}
{/foreach}
关于php - 使用 Smarty php 显示 mysql 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10126397/