Home  >  Article  >  Backend Development  >  Wrote a function_PHP tutorial

Wrote a function_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 10:59:40775browse

  /**
* Function data_box
* Function: Output the content in the data source according to the template
* Parameter
* $fun callback function, must be provided. Its function is to read data from the data source. The best thing to return is an associative array
* $source data source, must be provided. Can be an array or query result
* $template template, optional. Use standard tables to output data when no template is provided
* Template format:
* array(top=>"",block=>"",fool=>"")
* Among them:
* top beginning part
* The repeatable part of block, the variable is the key of the associative array, in the form of $IN_varname. The leading IN_ can be omitted
* end of fool
*/
function data_box($_fun,$_source,$_template="") {
$_ar = $_fun(&$_source);
if($_template == "") {
while(list($k,) = each($_ar)) {
$th .= "$k";
$td .= "$IN_$k";
}
$_template = array(top=>"$th",block=>"$td",fool=>"

");
}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=>"列表测试
"));

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/445594.htmlTechArticle/** * 函数 data_box * 功能 根据模板输出数据源中的内容 * 参数 * $fun 回调函数,必须提供。作用是从数据源中读取数据。要求返回的最好是关...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn