写了个函数
<?php<BR>/**<BR>* 函数 data_box<BR>* 功能 根据模板输出数据源中的内容<BR>* 参数<BR>* $fun 回调函数,必须提供。作用是从数据源中读取数据。要求返回的最好是关联数组<BR>* $source 数据源,必须提供。可以是数组或查询结果<BR>* $template 模板,可以没有。未提供模板时用标准表格输出数据<BR>* 模板格式:<BR>* array(top=>"",block=>"",fool=>"")<BR>* 其中:<BR>* top 开始部分<BR>* block 可重复部分,变量为关联数组的键,形如$IN_varname。其中前导的IN_可省略<BR>* fool 结束部分<BR>*/<BR>function data_box($_fun,$_source,$_template="") {<BR>$_ar = $_fun(&$_source);<BR>if($_template == "") {<BR>while(list($k,) = each($_ar)) {<BR>$th .= "<th>$k</th>";<BR>$td .= "<td>/$IN_$k</td>";<BR>}<BR>$_template = array(top=>"<table border><tr>$th</tr>",block=>"<tr>$td</tr>",fool=>"</table>");<BR>}else if(! preg_match("//$IN_/w+/",$_template[block]))<BR>$_template[block] = preg_replace("/[/$](/w*)/U","/$IN_//1",$_template[block]);<BR><BR>$buf = eval("return /"$_template[top]/";");<BR>do {<BR>extract($_ar, EXTR_PREFIX_ALL, "IN");<BR>$buf .= eval("return /"$_template[block]/";");<BR>}while($_ar = $_fun(&$_source));<BR>$buf .= eval("return /"$_template[fool]/";");<BR>return $buf;<BR>}<BR><BR>function get_data($source) {<BR>if(list($k,$v) = each($source))<BR>return $v;<BR>return false;<BR>}<BR><BR>$arr = array(<BR>array(a=>1,b=>2,c=>3,11,12,31),<BR>array(a=>11,b=>12,c=>13,11,12,131)<BR>);<BR><BR>echo data_box("get_data",$arr);<BR>echo data_box("get_data",$arr,array(top=>"列表测试<select>",block=>@#<option value=$a>$b@#,fool=>"</select><br>"));<BR><BR>$tpl = array(top=>"月历测试<table><tr bgcolor=@##000000@# style=@#color:#cfcfcf@#><th>日</th><th>一</th><th>二</th><th>三</th><th>四</th><th>五</th><th>六</th></tr>",block=>@#<tr><td>$0</td><td>$1</td><td>$2</td><td>$3</td><td>$4</td><td>$5</td><td>$6</td></tr>@#,fool=>"</table>");<BR><BR>$a = array_merge(array_fill(0,$w=date("w",mktime(0,0,0,date("m"),1,date("Y")))," "),range(1,$d=date("t")),array_fill(0,(7-($w+$d)%7)%7," "));<BR>for($i=0;$i<count($a);$i+=7)<BR>$ar[] = array_slice($a,$i,$i+7);<BR><BR>echo ereg_replace("<td>(".date("d")."</td>)","<td bgcolor=@##000000@# style=@#color:#ffffff@#>//1",data_box("get_data",$ar,$tpl));<BR><BR>$tpl = array(top=>"分页导航测试<br>",block=>@#共{$0}条[{$1}页] 第{$2}页 {$3} {$4} {$5} {$6}@#,fool=>"");<BR>$record = 20;<BR>$pagesize = 6;<BR>$pages = ceil($record/$pagesize);<BR>$page=2;<BR><BR>$ar = array(<BR>array($record,$pages,$page,<BR>$page>1?"首页":"",<BR>$page>1?"上页":"",<BR>$page<$pages?"下页":"",<BR>$page<$pages?"尾页":""<BR>)<BR>);<BR>echo data_box("get_data",$ar,$tpl);<BR>?><BR>页:
[1]