寫了個函數_PHP教程

來源:互聯網
上載者:User
/**
* 函數 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 .= "$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=>"列表測試",block=>'$b',fool=>"
"));

http://www.bkjia.com/PHPjc/445594.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/445594.htmlTechArticle/** * 函數 data_box * 功能 根據模板輸出資料來源中的內容 * 參數 * $fun 回呼函數,必須提供。作用是從資料來源中讀取資料。要求返回的最好是關...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.