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} 
这样,把数组循环显示出来!!!
我上面的代码无法实现,请高手帮忙解答。怎么用循环出来。

解决方案 »

  1.   

    问题没有说清楚, 打 “.....” 这里想怎么循环, 你写成php代码, 我帮你转换为模板试试
      

  2.   

    function newslist($params)
    {
    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循环.
      

  3.   


    <?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#}
      

  4.   

    感谢楼上的回答
    可是
    {#section name=Loop loop=$news#}
    中的$news是什么。
    $smarty->register_function('newslist',"newslist"); 
    应该是{newslist}啊
      

  5.   

    sorry,以上写法是循环一个数组.换成{newslist}试试~~
      

  6.   

    {newslist num=10} 这样就可以改变函数里$num的值,从而达到取固定条数的记录. -*----------------------------num 是什么, 从那里来的 ?
      

  7.   

    不行的。还有,num是function的变量。用Smarty Register_funtion可以达到修改function里变量的值你们可以看看手册里的register_function用法呀。~``