register_function 返回一个数组后,如果通过section或foreach循环举个例子比如 $smarty->register_function('newslist',"newslist"); function newslist(){
.....
while (....) {
$news[] = $result;
}
return $news //返回数组
}
在index.html里
{newslist}
获得这个数组
如何像
{section loop={newslist} name=loop}
...
{/section}
这样,把数组循环显示出来!!!
我上面的代码无法实现,请高手帮忙解答。怎么用循环出来。
.....
while (....) {
$news[] = $result;
}
return $news //返回数组
}
在index.html里
{newslist}
获得这个数组
如何像
{section loop={newslist} name=loop}
...
{/section}
这样,把数组循环显示出来!!!
我上面的代码无法实现,请高手帮忙解答。怎么用循环出来。
{
extract($params);
global $DB; //MySQL类实例
if (empty($num))
$num = 5;
$sql = "select * from `news` order by `id` DESC limit $num";
$query = $DB->query($sql) or die(mysql_error());
$row = $DB->fetch($query);
while ($row = $DB->fetch($query))
{
$news[] = $row;
}
unset($DB);
return $news;
}
$smarty->register_function('newslist',"newslist");
然后在模版调用时
{newslist num=10} 这样就可以改变函数里$num的值,从而达到取固定条数的记录.
但是,return $news这个数组后,如何用section循环.
<?php
function newslist($params){
extract($params);
global $DB; //MySQL类实例
$news = array();
if (empty($num)) $num = 5;
$sql = "select * from `news` order by `id` DESC limit $num";
$query = $DB->query($sql) or die(mysql_error());
while ($row = $DB->fetch($query))
{
$news['title'] = $row['title']; //假如title是表news一个字段
}
return $news;
}
$smarty->register_function('newslist',"newslist");
$smarty->display('test.tpl');
?>test.tpl{#section name=Loop loop=$news#}
{#$news.title#}
{#/section#}
可是
{#section name=Loop loop=$news#}
中的$news是什么。
$smarty->register_function('newslist',"newslist");
应该是{newslist}啊