Heim > Artikel > Backend-Entwicklung > 把历次都要执行的部分抽出来
把每次都要执行的部分抽出来
php中,共有3个函数,其中绝大分数是相同的。如何能更科学的变成一个函数,从而搞效率。
方法一:将此3个函数变成一个,怎么实现呢?
方法二:奖此3个函数的执行体,就是输出的部分公共部分,单独做个函数。如何实现呢?????
function wc1($sql){<br /> <br /> $tb='';<br /> $showArr=array();<br /> $q=$this->getAll3($sql);<br /> while ($r=$q->fetch_array(MYSQLI_USE_RESULT)) {<br /> $tb.='<tr><td>'.$r["number"].'</td>';<br /> $tb.=isset($this->show["customer"])?'<td>'.$r["customer"].'</td>':NULL;<br /> $tb.=isset($this->show["orderNo"])?'<td>'.$r["orderNo"].'</td>':NULL;<br /><br /><br /> }<br /> return $tb; <br />}<br />function wc2($sql){<br /> <br /> $tb='';<br /> $showArr=array();<br /> $q=$this->getAll3($sql);<br /> while ($r=$q->fetch_array(MYSQLI_USE_RESULT)) {<br /> $tb.='<tr><td>'.$r["number"].'</td>';<br /> $tb.=isset($this->show["customer"])?'<td>'.$r["customer"].'</td>':NULL;<br /> $tb.=isset($this->show["orderNo"])?'<td>'.$r["orderNo"].'</td>':NULL;<br /> $tb.=isset($this->show["name"])?'<td>'.$r["name"].'</td>':NULL;<br /> $tb.=isset($this->show["wc"])?'<td>'.$r["wc"].'</td>':NULL;//----------------此处有变化<br /><br /> }<br /> return $tb; <br />}<br />function wc3($sql){<br /> <br /> $tb='';<br /> $showArr=array();<br /> $q=$this->getAll3($sql);<br /> while ($r=$q->fetch_array(MYSQLI_USE_RESULT)) {<br /> $tb.='<tr><td>'.$r["number"].'</td>';<br /> $tb.=isset($this->show["customer"])?'<td>'.$r["customer"].'</td>':NULL;<br /> $tb.=isset($this->show["orderNo"])?'<td>'.$r["orderNo"].'</td>':NULL;<br /> $tb.=isset($this->show["name"])?'<td>'.$r["name"].'</td>':NULL;<br /> $tb.=isset($this->show["fsaww"])?'<td>'.$r["sfs3"].'</td>':NULL;//----------------此处有变化<br /><br /> }<br /> return $tb; <br />}
function wc($sql){<br /> $tb='';<br /> $showArr=array();<br /> $q=$this->getAll3($sql);<br /> $dict = array('fsaww' => 'sfs3'); //这里是对照表<br /> while ($r=$q->fetch_array(MYSQLI_USE_RESULT)) {<br /> $tb.='<tr><td>'.$r["number"].'</td>';<br /> foreach($this->show as $k) {<br /> if(isset($dict[$k])) $k = $dict[$k];<br /> $tb.='<td>'.$r[$k].'</td>';<br /> }<br /> }<br /> return $tb; <br />}
<?php <br />function wc1($sql, $assoc = array()){<br><br> $tb='';<br> $showArr = array();<br> $q=$this->getAll3($sql);<br> while ($r=$q->fetch_array(MYSQLI_USE_RESULT)) {<br> $tb.='<tr> <td>'.$r["number"].'</td>';<br> if(count($assoc)){<br> foreach($keys as $k => $v){<br> $tb .= isset($this->show[$k]) ?'<td>'.$r[$v].'</td>':NULL;<br> }<br> }<br> }<br> return $tb;<br>}<br><br>wc1($sql, array('customer' => 'customer', 'orderNo' => 'orderNo'));<br>wc1($sql, array('customer' => 'customer', 'orderNo' => 'orderNo', 'name' => 'name', 'wc' => 'wc'));<br>wc1($sql, array('customer' => 'customer', 'orderNo' => 'orderNo', 'name' => 'name', 'fsaww' => 'sfs3'));<div class="clear"> </div> </tr>