smarty 中数组循环嵌套调不是报错就是不显示 你的目的不是显示这样的结果吗?gender male 1 just do it ongender female 2 stop doing off 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在写程序的时候自己经常有这样的程序结构,不知道是不是这种结构不好,但是用smarty的时候老是不知道怎么来处理,所以一般遇到了都用php处理好然后assign到模板里面 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} 我想你可能是我没有说清楚,比如说我从数据库里面选出来一个用户列表,state字段用on,或者off表示,我又有另外一个常量数组stateArr,当索引是on的时候显示 just do it 当索引是off 的时候显示 stop doing ,我想用在smarty里面用用户的state字段的值做stateArr数组的索引,取出值 那你可以这样$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} 用foreach循环{foreach from=$userList item=item}gender {$sexArr[$item.sex]} {$stateArr[$item.state]}{/foreach} 天哪,非常感谢,其实你的写法我本来用差不多相同的方式试过{section name=idx loop=$userList} gender {$sexArr[$userList[idx].sex]} {$stateArr[$userList[idx].state]}<br/> {/section} 结果不能显示,最后得出来得结论就是smarty里面如果有2个[[]]里面再带上$标识符,基本上,就会显现出作为一个包装后的工具的缺点,灵活性差,不能正确表达意思,不知道是不是设计上的缺陷,非常谢谢johnpanq(飞花逐月) 和piner(nadmin 简称难民) 另外补充一下按照下面的写法,smarty是会报错的{section name=idx loop=$userList} gender {$sexArr[$userList[idx].sex]} {$stateArr[$userList[idx].state]}<br/> {/section} PHP链接的时候 为什么要用 var_dump这个变量输出? 将中英文字符串中的大写字母转换成小写字母 php如何获取文件夹数组--按时间排列? 无法上传文件。求解 RSS抓新闻 discuz 如何知道最后发表者的username便可以取得uid? 关于个性域名的问题,请高手帮忙! 高手指教,mysql事务回滚的问题! 如何把新闻主题和新闻内容写到数据库里去? PHP ADODB 数据库操作 请教个问题 === web开发的下一个学习方向:ajax ===
$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}
$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}
{foreach from=$userList item=item}
gender {$sexArr[$item.sex]} {$stateArr[$item.state]}
{/foreach}
{section name=idx loop=$userList}
gender {$sexArr[$userList[idx].sex]} {$stateArr[$userList[idx].state]}<br/>
{/section} 结果不能显示,最后得出来得结论就是smarty里面如果有2个[[]]里面再带上$标识符,基本上,就会显现出作为一个包装后的工具的缺点,灵活性差,不能正确表达意思,不知道是不是设计上的缺陷,非常谢谢johnpanq(飞花逐月) 和piner(nadmin 简称难民)
{section name=idx loop=$userList}
gender {$sexArr[$userList[idx].sex]} {$stateArr[$userList[idx].state]}<br/>
{/section}