【问题标题】:Show mysql query using Smarty php使用 Smarty php 显示 mysql 查询
【发布时间】:2012-04-12 15:08:25
【问题描述】:

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

项目的一个要求是使用 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 是如何做到的?

【问题讨论】:

  • 或许您应该在开始项目之前阅读 Smarty 文档。

标签: php mysql smarty


【解决方案1】:

先尝试阅读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}

【讨论】:

  • 谢谢,这正是我要找的 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多