0

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?

1
  • Perhaps you should read the Smarty documentation before starting your project. Commented Apr 12, 2012 at 15:47

1 Answer 1

4

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}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.