2012年2月1日 星期三

Smarty中使用foreach迴圈

在Smarty中若使用迴圈方式該如何使用?在Smarty中有兩種迴圈作法foreach與section,本次介紹foreach迴圈的做法。

PHP檔案
require_once('connMysql.php');
$sql_query="CALL sp_ap_sto010q01()";
$result=mysql_query($sql_query) or die(mysql_error());
$value=array();
while ($row_result = mysql_fetch_assoc($result))
{
   $value[] = $row_result;     
}
$smarty->assign('array',$value);

foreach from=陣列變數  item=在迴圈內代表目前元素的變數名稱
TPL檔案
{foreach from=$array item=row}
<tr><td>
<input type="hidden" name="itemno" title="{$row.ITEMNO}" value="{$row.ITEMNO}" />            
<input type="hidden" name="itemnm" title="{$row.ITEMNO}" value="{$row.ITEMNM}" />
<input type="hidden" name="invamt" title="{$row.ITEMNO}" value="{$row.INVAMT}" />
<input type="hidden" name="taxamt" title="{$row.ITEMNO}" value="{$row.TAXAMT}" />
<input type="hidden" name="h_periods" title="{$row.ITEMNO}" value="{$row.PERIODS}" />   
<input type="hidden" name="h_item" title="{$row.ITEMNO}" value="{$row.ITEM}" />           
<input name="os0" type="radio" title="{$row.ITEMNO}" value="{$row.ITEMNO}" />
{$row.YM}&nbsp;USD <br />
</td>                
</tr>
{/foreach}      

1 則留言: