I am trying to use smarty for a new website but I have a little problem assign data from a sql query to a smartyt foreach loop. I do not get any data from the query to my template.
In my class.designs.php I have this:
function listAllDesigns() {
global $db;
$row = $db->query("
SELECT ds.*, count(com.comment) AS countcom
FROM designs ds LEFT JOIN comments com ON com.design_id = ds.id
WHERE ds.approved = 1
GROUP BY ds.id
ORDER BY ds.date_added ASC")->resultset();
$smarty = new Smarty;
$smarty->assign('designs', $row);
return;
}
and my index.php
include_once("includes/connect.php"); //Database connection
include_once("includes/config.php"); //Configuration file
include_once("includes/classes/class.designs.php"); //Main design class
require('smarty/libs/Smarty.class.php');
$designs = new Designs();
$designs->listAllDesigns();
$smarty = new Smarty;
$smarty->debugging = true;
$smarty->caching = true;
$smarty->cache_lifetime = 120;
$smarty->assign("charset", $config->charset);
$smarty->assign("pagetitle", $config->pagetitle);
$smarty->display('index.tpl');
and in index.tpl I have this:
{foreach $designs as $r}
<li>{$r.name}</li>
{foreachelse}
No results
{/foreach}
It just print "No result" and I am getting a "Undefined index designs" error.
include_once("includes/connect.php"); //Database connection
include_once("includes/config.php"); //Configuration file
include_once("includes/classes/class.designs.php"); //Main design class
require('smarty/libs/Smarty.class.php');
$designs = new Designs();
$designs->listAllDesigns();
$smarty = new Smarty;
$smarty->debugging = true;
$smarty->caching = true;
$smarty->cache_lifetime = 120;
// TO USE some value ($designs in your case) at your template you must assign it
// 1st is its name and 2nd is the value
$smarty->assign("designs", $designs);
$smarty->assign("charset", $config->charset);
$smarty->assign("pagetitle", $config->pagetitle);
$smarty->display('index.tpl');
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.