0

我正在尝试将 smarty 用于新网站,但在将数据从 sql 查询分配到 smartyt foreach 循环时遇到了一点问题。我没有从查询中获得任何数据到我的模板。

在我的 class.designs.php 我有这个:

    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;        
        }

和我的 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');

在 index.tpl 我有这个:

{foreach $designs as $r}
  <li>{$r.name}</li>
{foreachelse}
   No results 
{/foreach}

它只是打印“无结果”,我收到“未定义的索引设计”错误。

4

1 回答 1

0
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');
于 2013-02-07T07:50:36.313 回答