php - 使用 Smarty php 显示 mysql 查询

标签 php mysql smarty

我正在将博客作为学校项目,但有点卡住了。

该项目的要求之一是使用 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/

相关文章:

PHP:带引号的评论回复系统。如何向子(回复)消息显示父(引用)消息

php - 计数、分组依据、内连接、php、mysql

php - foreach SMARTY 中的下一项

php - Composer 没有加载我自己的 psr-0 文件

php - 将我的 LAMP 移至亚马逊

运行 mysql 查询时的 Python 'TypeError: not enough arguments for format string'

javascript - 如何合并服务器端模板和 AJAX 以最大限度地提高可重用性

smarty - 是否可以将变量放入配置文件中?

php - 使用 htaccess 删除文件夹名称

php - 在 MySQL 中存储复选框值