smarty不能显示二维数组的问题

#1 fengyutonglu2

class main extends spController
{
        function right(){
                //if (empty($_SESSION["userinfo"])){
                //        $this->error("您的会话已超时,请重新登录!",spUrl("sa","login"));
                //}else{
                             $this->results='my first problem';//这里可以正常显示,请明smarty没有错
                     $this->abc = 'test abc';//这里也能正常显示
                     $this->ak[] = array("newsID"=>1, "newsTitle"=>"第1条新闻");//这个数组不能正常显示
                     $this->ak[] = array("newsID"=>2, "newsTitle"=>"第2条新闻");
                     $this->ak[] = array("newsID"=>3, "newsTitle"=>"第3条新闻");
                     $this->ak[] = array("newsID"=>4, "newsTitle"=>"第4条新闻");
                     $this->ak[] = array("newsID"=>5, "newsTitle"=>"第5条新闻");
                     $this->ak[] = array("newsID"=>6, "newsTitle"=>"第6条新闻");
            
            dump($this->$ak);//什么也不显示
            print_r($this->ak);  //什么也不显示
            print_r($this->results);//显示正常
                        $this->display("main_right.html");
                //}                        
               
        }
}

//main_right.html内容
  <{section name = index loop = $ak}>
            新闻编号:<{$ak[index].newsID}>

            新闻标题:<{$ak[index].newsTitle}>


  <{/section}>
  
// 一直找不到正确的显示方法,请高手帮看一下是哪里错了,谢谢

2010-05-03 11:45:45

#2 jake

你这里把section的使用看做了for,而正确的
section的循环使用方法:http://speedphp.com/doc/smarty/language.function.section.html
还有foreach的http://speedphp.com/doc/smarty/language.function.foreach.html
smarty中文手册:http://speedphp.com/doc/smarty/

2010-05-03 12:13:54

#3 fengyutonglu2

我把这个$this->ak[]拿出SPEEDPHP环境就可以dump()这个数组,但在SPEEDPHP中dump($this)里面就没有这个数组,用上面的section也可以在非speedphp环境中输出来。还希望jake老大给个正解。

2010-05-03 15:11:01

#4 jake

正解?不明白,smarty就是smarty,smarty的数组的显示就是使用smarty的方式,没有什么其他的方式,speedphp也没改smarty的东西。

而且上面的代码非常的乱,其实可以很简单的
$this->ak = array( // 这是正确的类变量的赋值方法
array('id'=>1,'title'=>'this is title'),
array('id'=>2,'title'=>'this is title again'),
);
dump($this->ak);

模板中使用:
<{foreach from=$ak item=myak}><{$myak.id}>:<{$myak.title}><{/foreach}>

这样就可以了。另外,你原本的代码是错误的,所以没有正确的结果,是很正常的。

2010-05-03 16:18:05

#5 yuanjianhua

$this->abc = 'test abc';//这里也能正常显示
                     $this->ak[] = array("newsID"=>1, "newsTitle"=>"第1条新闻");//这个数组不能正常显示
                     $this->ak[] = array("newsID"=>2, "newsTitle"=>"第2条新闻");
                     $this->ak[] = array("newsID"=>3, "newsTitle"=>"第3条新闻");
                     $this->ak[] = array("newsID"=>4, "newsTitle"=>"第4条新闻");
                     $this->ak[] = array("newsID"=>5, "newsTitle"=>"第5条新闻");
                     $this->ak[] = array("newsID"=>6, "newsTitle"=>"第6条新闻");


这里的赋值已经错了!

如果要像你那样,应该可以这么写:
  $ak[] = array("newsID"=>1, "newsTitle"=>"第1条新闻");
   $this->ak = $ak;

2010-05-03 17:47:09

#6 fengyutonglu2

哦,原来是数组赋值错了,谢谢Jake老大和yuanjianhua。呵呵。支持jake老大的热情

2010-05-03 18:45:02