I wrote a function.

Source: Internet
Author: User
Tags date array eval
function <?php


/**


* Function Data_box


* function is based on the template output data source content


* Parameter


* $fun callback function that must be provided. The role is to read data from the data source. The best thing to return is an associative array


* $source data source that must be provided. can be an array or query result


* $template template, can not be. Output data in a standard form when a template is not provided


* Template format:


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


* of which:


* Top Start part


* Block can be repeated, the variable is the key of the associative array, shaped like $in_varname. Where the leading in_ can be omitted


* Fool End part


*/


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=>) list test <select> ",block=> ' <option value= $a > $b ',fool=> "</select><br>"));





$tpl = Array (top=> "Monthly calendar test <table><tr bgcolor= ' #000000 ' style= ' color: #cfcfcf ' ><th> Day </th ><th> one </th><th> two </th><th> three </th><th> four </th><th> five </th ><th> six </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=> "Paging navigation test <br>",block=> "total {$} [{} pages] {$} {$} {$} {$} ',fool=>");


$record = 20;


$pagesize = 6;


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


$page = 2;





$ar = Array (


Array ($record, $pages, $page,


$page >1? " Home ":",


$page >1? " on page ":",


$page < $pages? " Next page ":",


$page < $pages? " Last ":" "


)


);


Echo data_box ("Get_data", $ar, $TPL);


?>








Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.