• 技术文章 >后端开发 >php教程

    把历次都要执行的部分抽出来

    2016-06-13 11:15:45原创310
    把每次都要执行的部分抽出来
    php中,共有3个函数,其中绝大分数是相同的。如何能更科学的变成一个函数,从而搞效率。
    方法一:将此3个函数变成一个,怎么实现呢?
    方法二:奖此3个函数的执行体,就是输出的部分公共部分,单独做个函数。如何实现呢?????

    function wc1($sql){

    $tb='';
    $showArr=array();
    $q=$this->getAll3($sql);
    while ($r=$q->fetch_array(MYSQLI_USE_RESULT)) {
    $tb.=''.$r["number"].'';
    $tb.=isset($this->show["customer"])?''.$r["customer"].'':NULL;
    $tb.=isset($this->show["orderNo"])?''.$r["orderNo"].'':NULL;


    }
    return $tb;
    }
    function wc2($sql){

    $tb='';
    $showArr=array();
    $q=$this->getAll3($sql);
    while ($r=$q->fetch_array(MYSQLI_USE_RESULT)) {
    $tb.=''.$r["number"].'';
    $tb.=isset($this->show["customer"])?''.$r["customer"].'':NULL;
    $tb.=isset($this->show["orderNo"])?''.$r["orderNo"].'':NULL;
    $tb.=isset($this->show["name"])?''.$r["name"].'':NULL;
    $tb.=isset($this->show["wc"])?''.$r["wc"].'':NULL;//----------------此处有变化

    }
    return $tb;
    }
    function wc3($sql){

    $tb='';
    $showArr=array();
    $q=$this->getAll3($sql);
    while ($r=$q->fetch_array(MYSQLI_USE_RESULT)) {
    $tb.=''.$r["number"].'';
    $tb.=isset($this->show["customer"])?''.$r["customer"].'':NULL;
    $tb.=isset($this->show["orderNo"])?''.$r["orderNo"].'':NULL;
    $tb.=isset($this->show["name"])?''.$r["name"].'':NULL;
    $tb.=isset($this->show["fsaww"])?''.$r["sfs3"].'':NULL;//----------------此处有变化

    }
    return $tb;
    }


    ------解决方案--------------------
    function wc($sql){
    $tb='';
    $showArr=array();
    $q=$this->getAll3($sql);
    $dict = array('fsaww' => 'sfs3'); //这里是对照表
    while ($r=$q->fetch_array(MYSQLI_USE_RESULT)) {
    $tb.=''.$r["number"].'';
    foreach($this->show as $k) {
    if(isset($dict[$k])) $k = $dict[$k];
    $tb.=''.$r[$k].'';
    }
    }
    return $tb;
    }

    ------解决方案--------------------
    function wc1($sql, $assoc = array()){

    $tb='';
    $showArr = array();
    $q=$this->getAll3($sql);
    while ($r=$q->fetch_array(MYSQLI_USE_RESULT)) {
    $tb.=''.$r["number"].'';
    if(count($assoc)){
    foreach($keys as $k => $v){
    $tb .= isset($this->show[$k]) ?''.$r[$v].'':NULL;
    }
    }
    }
    return $tb;
    }

    wc1($sql, array('customer' => 'customer', 'orderNo' => 'orderNo'));
    wc1($sql, array('customer' => 'customer', 'orderNo' => 'orderNo', 'name' => 'name', 'wc' => 'wc'));
    wc1($sql, array('customer' => 'customer', 'orderNo' => 'orderNo', 'name' => 'name', 'fsaww' => 'sfs3'));
    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:nbsp gt lt customer array
    VIP课程(WEB全栈开发)

    相关文章推荐

    • 【活动】充值PHP中文网VIP即送云服务器• 详细介绍PHP中时间处理类Carbon的用法• 分享一个neo4j(图形数据库)的PHP库!• 一文聊聊php5.4的特性【总结】• PHP常量两种定义方法:define和const有什么区别• PHP商城那个好? 2022年十大开源PHP商城【分享】
    1/1

    PHP中文网