首先建立一个html文件,创建表单。增加两个文本text表单用来传递数据,再建立一个php文件用来接收表单传递过来的数据,把传递过来的两个数值,从m到n利用for循环赋值给数组。再应用smarty模板,创建一个tpl文件。利用这个tpl文件显示php文件中的数组。
利用if语句判断,if $array[el]%$num_cate==0、判断这些数中是否能被要整除的数整除。
输出从m到n中的奇数与偶数。
.html
<!DOCTYPE html PUBLIC "-//W3C//DTDXHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<metahttp-equiv="Content-Type" content="text/html;charset=utf-8" />
<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(){
varnum = document.getElementById("c_num").value;
document.getElementById("num_scope").action="num.php?num="+num;
document.getElementById("num_scope").submit();
}
</script>
<title>无标题文档</title>
</head>
<body>
<form action=""method="post" id="num_scope">
请输入数值范围<inputtype="text" name="num_start" size="4" />到<input type="text"name="num_end" size="4" /><br />
</form>
<form action=""method="post" id="customer_num">
输出能被<inputid="c_num" type="text" size="4" />整除的数
</form>
<input value="输出"type="button" onclick="submit_else()"/>
<input value="输出偶数" type="button"onclick="submit_even()" />
<input value="输出奇数" type="button"onclick="submit_odd()" />
</body>
</html>
______________________________________________________________________
.php
<?php
$start = $_POST["num_start"];
$end = $_POST["num_end"];
$num_cate = $_GET["num"];
$array = array();
$count = $end-$start;
/*for($i=0;$i<=$count;$i++){
$array[$i]=$start;
$start++;
}*/
for($i=$start;$i<=$end;$i++){
$array[]=$i;
}
include("Smarty/libs/Smarty.class.php");
$smarty = new Smarty();
$smarty->reInitSmarty("Smarty/demo/templates","Smarty/demo/templates_c","Smarty/demo/configs");
$smarty->assign("array",$array);
$smarty->assign("num_cate",$num_cate);
$smarty->display("num.tpl");
?>
_______________________________________________________________________________
.tpl
<{if $num_cate eq "even"}>
<{foreach from=$array item=valuename="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>