smarty中用section如何处理两层循环?
例如:
index.php$tid=1;
$sql = "SELECT * FROM question WHERE tid=$tid LIMIT 3";
$query = $db->query($sql);
while ($row = $db->fetch_array($query)){
$osql = "select * from `option` where tid=$tid AND QId={$row['qid']}";
$oquery = $db->query($osql);
while ($orow = $db->fetch_array($query)){
$oarray[] = array(
"oid" => $orow["oid"],
"ocontent" => $orow["ocontent"]
);
}
$array[] = array(
"qid"=>$row["qid"],
"qcontent"=>$row["qcontent"],
"option"=>$oarray
);
}
$smarty->assign("V_Q", $array);
unset($array);index.tpl
<{section name=loop loop=$V_Q}>
<TR><TD>
<TABLE cellSpacing=0 cellPadding=0 width="100%" align=center border=0>
    <TBODY>
      <TR>
        <TD valign=top width=36 ><{$smarty.section.loop.index+1}>.</TD>
        <TD class="tit2" width=714  align="left"><STRONG><{$V_Q[loop].qcontent}></STRONG></TD>
      </TR>
      <TR>
        <TD>&nbsp;</TD>
<TD>
<TABLE cellSpacing=1 cellPadding=3 width="100%" bgColor=#e0e0e0 border=0>
<TBODY>
                    <{section name=oloop oloop=$V_Q[loop].option}>
<TR bgColor=#ffffff>
<TD align="left">
<input type="radio" name="rgQu_<{$V_Q[loop].qid}>" value="<{$V_Q[loop].option[oloop].oid}>"/>
<{$V_Q[loop].option[oloop].ocontent}> 
   </TD>
</TR>
<{/section}>
</TBODY>
</TABLE>
</TD>
      </TR>
    </TBODY>
</TABLE></TD></TR>
<{/section}>这样写当然是错误的,想请教下正确的写法是怎样的?

解决方案 »

  1.   

    你直接用php代码写不就ok了。。index.tpl里面。同样可以用<?php ?>来插入php代码。或者你看看生成后的php代码里的情况。就知道改怎么修改了。
      

  2.   


    <{section name=loop loop=$V_Q}> 
      <{$V_Q[loop].qcontent}> 
      ...
      <{foreach from = $V_Q[loop].option item=value key=id }>
       <{$value.oid}>
      <{/foreach}>
    <{/section}>第二种循环可以用foreach
      

  3.   

    我前段时间还正好写了这个问题的帖子。看看这里:http://www.rsywx.net/wordpress/2008/10/863
      

  4.   

    section可以嵌套
    例子如下:{|section name=key loop=$classKeyIndex|}
    <TR>
        <TD>{|$classKeyIndex[key].name|}</TD>
    </TR>
    <TR><TD align="left">
        {|section name=class loop=$classKeyIndex[key].detial|}
            <A href="">{|$classKeyIndex[key].detial[class].name|}</A>
        {|/section|}
    </TD></TR>
    <TR><TD height="9"></TD></TR>
    {|/section|}
      

  5.   

    section 的原型是for 照着for嵌套就好了