简体   繁体   中英

Show mysql query using Smarty php

I'm making a blog as a school project and is a bit stuck.

One of the requirements for the project is to use smarty, and this is totally new to me.

My problem is that I want to assign the "blog-posts" from my database to smarty variables. My approach is like this:

<?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"]);}
?>

Now i just print out "forfatter" from "Innlegg". How is this done by using smarty?

Try reading the Smarty FAQ first.. its very straightforward

$list=array();
while ($row = $result->fetch_assoc()) {
    $list[]=$row;
}

$smarty = new Smarty(); //maybe some configuration ?
$smarty->assign('list', $list);
$smarty->fetch('index.tpl');

and inside smarty index.tpl template file

{foreach from=$list item=row}
    <h1>{$row.forfatter}</h1>
    {$row.innhold}
{/foreach}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM