0
我想用smarty來建立一個新的網站,但我有一個小問題,從sql查詢分配數據到smartyt foreach循環。我沒有從查詢中獲取任何數據到我的模板。在smarty中獲取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}
它只是打印「沒有結果」,我得到了「未定義的索引設計」錯誤。
嗯..這似乎沒有任何區別。我仍然沒有結果。如果我var_dump($ row)在class.designs.php中,我可以看到數據庫中的完整數組 - 所以我知道我正在獲取數據。 –
Arh ..通過在listAllDesigns函數中設置_return $ row; _使其工作,然後實現您的解決方案。那樣做了! 謝謝! –