你的目的不是显示这样的结果吗?gender male 1 just do it on
gender female 2 stop doing off

解决方案 »

  1.   

    在写程序的时候自己经常有这样的程序结构,不知道是不是这种结构不好,但是用smarty的时候老是不知道怎么来处理,所以一般遇到了都用php处理好然后assign到模板里面
      

  2.   

    test.php:$userList[0]['sex']=1; 
    $userList[0]['state']='on'; 
    $userList[1]['sex']=2; 
    $userList[1]['state']='off'; $userList[0]['sexArr']='male'; 
    $userList[1]['sexArr']='female'; $userList[0]['stateArr']='just do it'; 
    $userList[1]['stateArr']='stop doing'; $smarty->assign("userList",$userList);test.tpl:{section name=idx loop=$userList} 
    gender {$userList[idx].sexArr} {$userList[idx].sex} {$userList[idx].stateArr} {$userList[idx].state}<br> 
    {/section} 
      

  3.   

    我想你可能是我没有说清楚,比如说我从数据库里面选出来一个用户列表,state字段用on,或者off表示,我又有另外一个常量数组stateArr,当索引是on的时候显示 just do it 当索引是off 的时候显示 stop doing ,我想用在smarty里面用用户的state字段的值做stateArr数组的索引,取出值
      

  4.   

    那你可以这样
    $sexArr[1]='male'; 
    $sexArr[2]='female'; $stateArr['on']='just do it'; 
    $stateArr['of']='stop doing';  //从数据库取出的数据
    while($row = mysql_fetch_array($result))
    {
      $userList[] = array(
                       'sex'   => $sexArr[$row[sex]],
                       'state' => $stateArr[$row[state]]
      );
    }
    $smarty->assign("userList",$userList); 
    $smarty->display("test.tpl");
    {section name=idx loop=$userList} 
    gender {$userList[idx].sex} {$userList[idx].state}<br> 
    {/section} 
      

  5.   

    用foreach循环
    {foreach from=$userList item=item}
    gender {$sexArr[$item.sex]} {$stateArr[$item.state]}
    {/foreach}
      

  6.   

    天哪,非常感谢,其实你的写法我本来用差不多相同的方式试过
    {section name=idx loop=$userList} 
    gender {$sexArr[$userList[idx].sex]} {$stateArr[$userList[idx].state]}<br/> 
    {/section} 结果不能显示,最后得出来得结论就是smarty里面如果有2个[[]]里面再带上$标识符,基本上,就会显现出作为一个包装后的工具的缺点,灵活性差,不能正确表达意思,不知道是不是设计上的缺陷,非常谢谢johnpanq(飞花逐月) 和piner(nadmin 简称难民)
      

  7.   

    另外补充一下按照下面的写法,smarty是会报错的
    {section name=idx loop=$userList} 
    gender {$sexArr[$userList[idx].sex]} {$stateArr[$userList[idx].state]}<br/> 
    {/section}