0

新しい Web サイトに 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 に答える