smarty模板--foreach迴圈二維陣列
阿新 • • 發佈:2019-01-30
require_once './libs/Smarty.class.php';
// $title="你好";
$arr=array('a'=>'北京','b'=>'上海','c'=>'天津');
$arr1=array(array('哈爾濱','上海'),array('北京','天津'));
$arr2=array(array('a'=>'北京','b'=>'上海','c'=>'天津'),array('c'=>'c北京','d'=>'d上海','c'=>'天津'));
$emplist=array(array('id'=>100,'name'=>"小明","age"=>40),array("id"=>'110',"name"=>"大明","age"=>80),array("id"=>111,"name"=>"中明","age"=>60));
$smarty = new Smarty();
$smarty->assign("arr",$arr);
$smarty->assign("arr1",$arr1);
$smarty->assign("arr2",$arr2);
$smarty->assign("emplist",$emplist);
$smarty->assign("if",5);
$smarty->display("text.tpl");
?>
.tpl 頁面
<br/><h1>*從from中取出陣列指向temp*</h1><br/>
{foreach from=$arr item=temp key=lx}
{$lx}={$temp}
{/foreach}
<br/><h1>***取出二維陣列***</h1><br/>
{foreach from=$arr1 item=temp}
{foreach from=$temp item=val}
{$val}
{/foreach}<br/>
{/foreach}
<br/><h1>***取出二維關聯陣列***</h1><br/>
{foreach from=$arr2 item=temp}
{foreach from=$temp item=val key=lx}
{$lx}={$val}
{/foreach}<br/>
{/foreach}
{foreach from=$emplist item=temp}
{foreach from=$temp item=val key=lx}
{if $lx!=age}
{$lx}={$val}
{/if}
{/foreach}<br/>
{/foreach}
<br/><h1>***IFELSE***</h1><br/>
{if $if>10}
a>10
{else}
a<10
{/if}
{foreach from=$emplist item=temp}
{if $temp.age<50}
小孩{$temp.name}年齡為{$temp.age}<br/>
{elseif50<$temp.age&&$temp.age<70}
青年朋友{$temp.name}年齡為{$temp.age}
{else}
老年朋友{$temp.name}年齡為{$temp.age}<br/>
{/if}
{/foreach}
// $title="你好";
$arr=array('a'=>'北京','b'=>'上海','c'=>'天津');
$arr1=array(array('哈爾濱','上海'),array('北京','天津'));
$arr2=array(array('a'=>'北京','b'=>'上海','c'=>'天津'),array('c'=>'c北京','d'=>'d上海','c'=>'天津'));
$emplist=array(array('id'=>100,'name'=>"小明","age"=>40),array("id"=>'110',"name"=>"大明","age"=>80),array("id"=>111,"name"=>"中明","age"=>60));
$smarty = new Smarty();
$smarty->assign("arr",$arr);
$smarty->assign("arr1",$arr1);
$smarty->assign("arr2",$arr2);
$smarty->assign("emplist",$emplist);
$smarty->assign("if",5);
$smarty->display("text.tpl");
?>
.tpl 頁面
<br/><h1>*從from中取出陣列指向temp*</h1><br/>
{foreach from=$arr item=temp key=lx}
{$lx}={$temp}
{/foreach}
<br/><h1>***取出二維陣列***</h1><br/>
{foreach from=$arr1 item=temp}
{foreach from=$temp item=val}
{$val}
{/foreach}<br/>
{/foreach}
<br/><h1>***取出二維關聯陣列***</h1><br/>
{foreach from=$arr2 item=temp}
{foreach from=$temp item=val key=lx}
{$lx}={$val}
{/foreach}<br/>
{/foreach}
{foreach from=$emplist item=temp}
{foreach from=$temp item=val key=lx}
{if $lx!=age}
{$lx}={$val}
{/if}
{/foreach}<br/>
{/foreach}
<br/><h1>***IFELSE***</h1><br/>
{if $if>10}
a>10
{else}
a<10
{/if}
{foreach from=$emplist item=temp}
{if $temp.age<50}
小孩{$temp.name}年齡為{$temp.age}<br/>
{elseif50<$temp.age&&$temp.age<70}
青年朋友{$temp.name}年齡為{$temp.age}
{else}
老年朋友{$temp.name}年齡為{$temp.age}<br/>
{/if}
{/foreach}