Home > Backend Development > PHP Tutorial > 把历次都要执行的部分抽出来

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

WBOY
Release: 2016-06-13 11:15:45
Original
859 people have browsed it

把每次都要执行的部分抽出来
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 />}
Copy after login


------解决方案--------------------
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 />}
Copy after login

------解决方案--------------------

<?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>
Copy after login
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template