num.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>foreach与section的应用</title>
<script language="javascript" type="text/javascript">
function submit_even(){
document.getElementById('num_scope').action="num.php?num=even";
document.getElementById('num_scope').submit();
}
function submit_odd(){
document.getElementById('num_scope').action="num.php?num=odd";
document.getElementById('num_scope').submit();
}
function submit_else(){
var num= document.getElementById('c_num').value;
document.getElementById('num_scope').action="num.php?num="+num;
document.getElementById('num_scope').submit();
}
</script>
</head>
<body>
<form action="" method="post" id="num_scope">
请输如数字范围:
从:<input type="text" name="num_start" size="4" />到<input type="text" name="num_end" size="4" /><br />
</form>
<form action="" method="post" id="customer_num">
输出能被<input type="text" name="num" id="c_num" size="6" />整除的数:
</form>
<input type="button" value="整除输出" οnclick="submit_else()" />
<input type="button" value="列出所有的偶数" οnclick="submit_even()" />
<input type="button" value="列出所有的奇数" οnclick="submit_odd()" />
</body>
</html>
num.php
<?php
$start = $_POST['num_start'];
$end = $_POST['num_end'];
$num_cate = $_GET["num"];
$array = array();
for($i=$start;$i<=$end;$i++){
$array[]=$i;
}
//print_r($array);
include 'libs/Smarty.class.php';
$smarty=new Smarty();
$smarty->template_dir="demo/templates";
$smarty->compile_dir="demo/templates_c";
$smarty->config_dir="demo/config";
$smarty->left_delimiter="<{";
$smarty->right_delimiter="}>";
$smarty->assign('array',$array);
$smarty->assign('num_cate',$num_cate);
$smarty->display('num.tpl');
num.tpl
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>foreach与section的应用</title>
</head>
<body>
<{if $num_cate eq "even"}>
<{foreach from=$array item=value name="e"}>
<{if $value is even}>
数组中第<{$smarty.foreach.e.iteration}>个偶数是:<{$value}><br />
<{/if}>
<{/foreach}>
<{elseif $num_cate eq "odd"}>
<{section name="odd" loop=$array}>
<{if $array[odd] is odd}>
数组中第<{$smarty.section.odd.index}>个奇数是:<{$array[odd]}><br />
<{/if}>
<{/section}>
<{else}>
<{section name="el" loop=$array}>
<{if $array[el]%$num_cate==0}>
数组中第<{$smarty.section.el.index}>个能被<{$num_cate}>整除的数是:<{$array[el]}><br />
<{/if}>
<{/section}>
<{/if}>
</body>
</html>