欢迎光临
我们一直在努力

写了个函数-PHP教程,PHP函数

建站超值云服务器,限时71元/月

<?php

/**

* 函数 data_box

* 功能 根据模板输出数据源中的内容

* 参数

* $fun 回调函数,必须提供。作用是从数据源中读取数据。要求返回的最好是关联数组

* $source 数据源,必须提供。可以是数组或查询结果

* $template 模板,可以没有。未提供模板时用标准表格输出数据

* 模板格式:

* array(top=>"",block=>"",fool=>"")

* 其中:

* top 开始部分

* block 可重复部分,变量为关联数组的键,形如$in_varname。其中前导的in_可省略

* fool 结束部分

*/

function data_box($_fun,$_source,$_template="") {

$_ar = $_fun(&$_source);

if($_template == "") {

while(list($k,) = each($_ar)) {

$th .= "<th>$k</th>";

$td .= "<td>\$in_$k</td>";

}

$_template = array(top=>"<table border><tr>$th</tr>",block=>"<tr>$td</tr>",fool=>"</table>");

}else if(! preg_match("/\$in_\w+/",$_template[block]))

$_template[block] = preg_replace("/[\$](\w*)/u","\$in_\\1",$_template[block]);

$buf = eval("return \"$_template[top]\";");

do {

extract($_ar, extr_prefix_all, "in");

$buf .= eval("return \"$_template[block]\";");

}while($_ar = $_fun(&$_source));

$buf .= eval("return \"$_template[fool]\";");

return $buf;

}

function get_data($source) {

if(list($k,$v) = each($source))

return $v;

return false;

}

$arr = array(

array(a=>1,b=>2,c=>3,11,12,31),

array(a=>11,b=>12,c=>13,11,12,131)

);

echo data_box("get_data",$arr);

echo data_box("get_data",$arr,array(top=>"列表测试<select>",block=><option value=$a>$b,fool=>"</select><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>");

$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," "));

for($i=0;$i<count($a);$i+=7)

$ar[] = array_slice($a,$i,$i+7);

echo ereg_replace("<td>(".date("d")."</td>)","<td bgcolor=#000000 style=color:#ffffff>\\1",data_box("get_data",$ar,$tpl));

$tpl = array(top=>"分页导航测试<br>",block=>共{$0}条[{$1}页] 第{$2}页 {$3} {$4} {$5} {$6},fool=>"");

$record = 20;

$pagesize = 6;

$pages = ceil($record/$pagesize);

$page=2;

$ar = array(

array($record,$pages,$page,

$page>1?"首页":"",

$page>1?"上页":"",

$page<$pages?"下页":"",

$page<$pages?"尾页":""

)

);

echo data_box("get_data",$ar,$tpl);

?>

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 写了个函数-PHP教程,PHP函数
分享到: 更多 (0)

相关推荐

  • 暂无文章