首頁 >後端開發 >php教程 >怎么简写代码

怎么简写代码

WBOY
WBOY原創
2016-06-23 14:38:181188瀏覽

if($_MODULE[huoqubaobei]=="2"){$arr = explode(',',$_MODULE[item_1]);  foreach($arr as $value){		  		   $item=$itemManager-> queryById ($value);		       $itemUrl = $uriManager->detailURI($item);                 $itemPicUrl = $item->getPicUrl(160);               $itemTitle = $item->title;                 $itemPrice = $item->price;                $itemsoldCount = $item->soldCount;								echo $itemUrl;				echo $itemPicUrl;				echo $itemTitle;				echo $itemPrice;				echo $itemsoldCount;						   }}else if($_MODULE[huoqubaobei]=="1"){		  $items=$itemManager->queryByKeyword("","hotsell",9);			 				  foreach($items as $item ){							       $itemUrl = $uriManager->detailURI($item);                 $itemPicUrl = $item->getPicUrl(160);               $itemTitle = $item->title;                 $itemPrice = $item->price;                $itemsoldCount = $item->soldCount; 					echo $itemUrl;				echo $itemPicUrl;				echo $itemTitle;				echo $itemPrice;				echo $itemsoldCount; 				   }}else if($_MODULE[huoqubaobei]=="3"){	   $items = $itemManager->queryByCategory($categoryId,"hotsell",10);     foreach($items as $item){		       $itemUrl = $uriManager->detailURI($item);                 $itemPicUrl = $item->getPicUrl(160);               $itemTitle = $item->title;                 $itemPrice = $item->price;                $itemsoldCount = $item->soldCount; 					echo $itemUrl;				echo $itemPicUrl;				echo $itemTitle;				echo $itemPrice;				echo $itemsoldCount;		 }}else{	     $itemUrl ="#";                 $itemPicUrl = "assets/images/nvtp.jpg";               $itemTitle = "请添加宝贝描述请添加宝贝描述请添加宝贝描述请";                 $itemPrice = 628;                $itemsoldCount =89; 					echo $itemUrl;				echo $itemPicUrl;				echo $itemTitle;				echo $itemPrice;				echo $itemsoldCount;}


怎么简写哈 
echo $itemPicUrl;
echo $itemTitle;
echo $itemPrice;
echo $itemsoldCount;
怎么不让他们重复写

我本来想都加到数组里 然后循环 但这样运行性能不行   谁有更好的方法




回复讨论(解决方案)

你在每个分支里输出同样的一组变量?
为何不在条件结构出来后在输出?

if($_MODULE[huoqubaobei]=="2"){	$arr = explode(',',$_MODULE[item_1]);	foreach($arr as $value)	{	    $items=$itemManager-> queryById ($value);    }}elseif($_MODULE[huoqubaobei]=="1"){      $items=$itemManager->queryByKeyword("","hotsell",9);}elseif($_MODULE[huoqubaobei]=="3"){      $items = $itemManager->queryByCategory($categoryId,"hotsell",10);}if($items) {	foreach($items as $item)	{	    $itemUrl = $uriManager->detailURI($item); 		$itemPicUrl = $item->getPicUrl(160);	    $itemTitle = $item->title;		$itemPrice = $item->price;		$itemsoldCount = $item->soldCount; 		echo $itemUrl;		echo $itemPicUrl;		echo $itemTitle;		echo $itemPrice;		echo $itemsoldCount;	 }}else{	$itemUrl ="#";     $itemPicUrl = "assets/images/nvtp.jpg";    $itemTitle = "请添加宝贝描述请添加宝贝描述请添加宝贝描述请";	$itemPrice = 628;	$itemsoldCount =89; 	echo $itemUrl;	echo $itemPicUrl;	echo $itemTitle;	echo $itemPrice;	echo $itemsoldCount;}

if($_MODULE[huoqubaobei]=="2"){	$arr = explode(',',$_MODULE[item_1]);	foreach($arr as $value)	{	    $items=$itemManager-> queryById ($value);    }}elseif($_MODULE[huoqubaobei]=="1"){      $items=$itemManager->queryByKeyword("","hotsell",9);}elseif($_MODULE[huoqubaobei]=="3"){      $items = $itemManager->queryByCategory($categoryId,"hotsell",10);}if($items) {	foreach($items as $item)	{	    $itemUrl = $uriManager->detailURI($item); 		$itemPicUrl = $item->getPicUrl(160);	    $itemTitle = $item->title;		$itemPrice = $item->price;		$itemsoldCount = $item->soldCount; 		echo $itemUrl;		echo $itemPicUrl;		echo $itemTitle;		echo $itemPrice;		echo $itemsoldCount;	 }}else{	$itemUrl ="#";     $itemPicUrl = "assets/images/nvtp.jpg";    $itemTitle = "请添加宝贝描述请添加宝贝描述请添加宝贝描述请";	$itemPrice = 628;	$itemsoldCount =89; 	echo $itemUrl;	echo $itemPicUrl;	echo $itemTitle;	echo $itemPrice;	echo $itemsoldCount;}


哈哈 你还是重复了  最后面

if($_MODULE[huoqubaobei]=="2"){	$arr = explode(',',$_MODULE[item_1]);	foreach($arr as $value)	{	    $items=$itemManager-> queryById ($value);    }}elseif($_MODULE[huoqubaobei]=="1"){      $items=$itemManager->queryByKeyword("","hotsell",9);}elseif($_MODULE[huoqubaobei]=="3"){      $items = $itemManager->queryByCategory($categoryId,"hotsell",10);}if($items) {	foreach($items as $item)	{	    $itemUrl = $uriManager->detailURI($item); 		$itemPicUrl = $item->getPicUrl(160);	    $itemTitle = $item->title;		$itemPrice = $item->price;		$itemsoldCount = $item->soldCount; 		echo $itemUrl;		echo $itemPicUrl;		echo $itemTitle;		echo $itemPrice;		echo $itemsoldCount;	 }}else{	$itemUrl ="#";     $itemPicUrl = "assets/images/nvtp.jpg";    $itemTitle = "请添加宝贝描述请添加宝贝描述请添加宝贝描述请";	$itemPrice = 628;	$itemsoldCount =89; 	echo $itemUrl;	echo $itemPicUrl;	echo $itemTitle;	echo $itemPrice;	echo $itemsoldCount;}

而且 我最后面的else 其实是elseif($_MODULE[huoqubaobei]=="4")

你在每个分支里输出同样的一组变量?
为何不在条件结构出来后在输出? 不明白你的意思。。。


你在每个分支里输出同样的一组变量?
为何不在条件结构出来后在输出? 不明白你的意思。。。

意思就是,你看每?if else?面都有
              echo $itemUrl;
                echo $itemPicUrl;
                echo $itemTitle;
                echo $itemPrice;
                echo $itemsoldCount; 
可以在?束位置加上,不需要在每?if else?面?。

	$response= '';	switch($_MODULE[huoqubaobei]){		case "2":			$arr = explode(',',$_MODULE[item_1]);			foreach($arr as $value){				$item=$itemManager-> queryById ($value);				$items = array_push($items, $item);			}		case "1":			$items = $itemManager->queryByKeyword("","hotsell",9);		case "3":			$items = $itemManager->queryByCategory($categoryId,"hotsell",10);		default:			$items = array();	}	if($items){		foreach($items as $item){			$itemUrl = $uriManager->detailURI($item); 			$itemPicUrl = $item->getPicUrl(160);			$itemTitle = $item->title;			$itemPrice = $item->price;			$itemsoldCount = $item->soldCount; 			$response .= $itemUrl.$itemPicUrl.$itemTitle.$itemPrice.$itemsoldCount		}	}else{			$itemUrl ="#"; 			$itemPicUrl = "assets/images/nvtp.jpg";			$itemTitle = "请添加宝贝描述请添加宝贝描述请添加宝贝描述请";			$itemPrice = 628;			$itemsoldCount =89;			$response .= $itemUrl.$itemPicUrl.$itemTitle.$itemPrice.$itemsoldCount	}	echo $response;

修改了一下,??才?。

	$response= '';	$items = array();	switch($_MODULE[huoqubaobei]){		case "2":			$arr = explode(',',$_MODULE[item_1]);			foreach($arr as $value){				$item=$itemManager-> queryById ($value);				array_push($items, $item);			}		case "1":			$items = $itemManager->queryByKeyword("","hotsell",9);		case "3":			$items = $itemManager->queryByCategory($categoryId,"hotsell",10);	}	if($items){		foreach($items as $item){			$itemUrl = $uriManager->detailURI($item); 			$itemPicUrl = $item->getPicUrl(160);			$itemTitle = $item->title;			$itemPrice = $item->price;			$itemsoldCount = $item->soldCount; 			$response .= $itemUrl.$itemPicUrl.$itemTitle.$itemPrice.$itemsoldCount		}	}else{			$itemUrl ="#"; 			$itemPicUrl = "assets/images/nvtp.jpg";			$itemTitle = "请添加宝贝描述请添加宝贝描述请添加宝贝描述请";			$itemPrice = 628;			$itemsoldCount =89;			$response .= $itemUrl.$itemPicUrl.$itemTitle.$itemPrice.$itemsoldCount	}	echo $response;

这段代码其实应该写成类并继承$itemManager的类(或组成对象链)会更好

1、形如

echo $itemUrl;echo $itemPicUrl;echo $itemTitle;echo $itemPrice;echo $itemsoldCount;
的可简化为
show($itemUrl, $itemPicUrl, $itemTitle, $itemPrice, $itemsoldCount);function show() {  echo join('', func_get_args());}
函数 show 中可做各种美化工作

2、原式就可写作
if($_MODULE[huoqubaobei]=="2"){$arr = explode(',',$_MODULE[item_1]);	 foreach($arr as $value){		$item=$itemManager-> queryById ($value);		show($uriManager->detailURI($item),			$item->getPicUrl(160),			$item->title,			$item->price,			$item->soldCount			);	}}else if($_MODULE[huoqubaobei]=="1"){	$items=$itemManager->queryByKeyword("","hotsell",9);	foreach($items as $item ){		show($uriManager->detailURI($item),			$item->getPicUrl(160),			$item->title,			$item->price,			$item->soldCount			);	}}else if($_MODULE[huoqubaobei]=="3"){	$items = $itemManager->queryByCategory($categoryId,"hotsell",10);	foreach($items as $item){		show($uriManager->detailURI($item),			$item->getPicUrl(160),			$item->title,			$item->price,			$item->soldCount			);	}}else{	show("#",		"assets/images/nvtp.jpg",		"请添加宝贝描述请添加宝贝描述请添加宝贝描述请",		628,		89,		);}


3、考虑到前三个分支存在相同的取数方法,于是可以再写一个函数
function item_show($items, $uriManager) {	foreach($items as $item){		show($uriManager->detailURI($item),			$item->getPicUrl(160),			$item->title,			$item->price,			$item->soldCount			);	}}


4、代码就进一步变为
if($_MODULE[huoqubaobei]=="2"){$arr = explode(',',$_MODULE[item_1]);	 foreach($arr as $value){		items_show(array($itemManager->queryById($value)), $uriManager);	}}else if($_MODULE[huoqubaobei]=="1"){	items_show($itemManager->queryByKeyword("","hotsell",9), , $uriManager);}else if($_MODULE[huoqubaobei]=="3"){	items_show($itemManager->queryByCategory($categoryId,"hotsell",10), $uriManager);}else{	show("#",		"assets/images/nvtp.jpg",		"请添加宝贝描述请添加宝贝描述请添加宝贝描述请",		628,		89,		);}function show() {	echo join('', func_get_args());}function items_show($items, $uriManager) {	foreach($items as $item){		show($uriManager->detailURI($item),			$item->getPicUrl(160),			$item->title,			$item->price,			$item->soldCount			);	}}

哈哈 最后一楼是对的

修改了一下,??才?。

	$response= '';	$items = array();	switch($_MODULE[huoqubaobei]){		case "2":			$arr = explode(',',$_MODULE[item_1]);			foreach($arr as $value){				$item=$itemManager-> queryById ($value);				array_push($items, $item);			}		case "1":			$items = $itemManager->queryByKeyword("","hotsell",9);		case "3":			$items = $itemManager->queryByCategory($categoryId,"hotsell",10);	}	if($items){		foreach($items as $item){			$itemUrl = $uriManager->detailURI($item); 			$itemPicUrl = $item->getPicUrl(160);			$itemTitle = $item->title;			$itemPrice = $item->price;			$itemsoldCount = $item->soldCount; 			$response .= $itemUrl.$itemPicUrl.$itemTitle.$itemPrice.$itemsoldCount		}	}else{			$itemUrl ="#"; 			$itemPicUrl = "assets/images/nvtp.jpg";			$itemTitle = "请添加宝贝描述请添加宝贝描述请添加宝贝描述请";			$itemPrice = 628;			$itemsoldCount =89;			$response .= $itemUrl.$itemPicUrl.$itemTitle.$itemPrice.$itemsoldCount	}	echo $response;


$itemUrl ="#"; 
        $itemPicUrl = "assets/images/nvtp.jpg";
        $itemTitle = "请添加宝贝描述请添加宝贝描述请添加宝贝描述请";
        $itemPrice = 628;
        $itemsoldCount =89;
        $response .= $itemUrl.$itemPicUrl.$itemTitle.$itemPrice.$itemsoldCount
怎么写到循环上

最後一?情?不是不用循???
如果要循?,需要提供循?的次?。

最後一?情?不是不用循???
如果要循?,需要提供循?的次?。

 foreach($items as $item){ 次数跟他一样

最后一?情?,你的items都?有?取,如何循??

最后一?情?,你的items都?有?取,如何循??

其实他前面if($items)判断是不对的不能这样判断

我最后面是else if($_MODULE[huoqubaobei]=="4") 判断的 

没看到有什么东西要循环的。。
else
{
         $itemUrl ="#"; 
                $itemPicUrl = "assets/images/nvtp.jpg";
               $itemTitle = "请添加宝贝描述请添加宝贝描述请添加宝贝描述请";
                 $itemPrice = 628;
                $itemsoldCount =89; 
                    echo $itemUrl;
                echo $itemPicUrl;
                echo $itemTitle;
                echo $itemPrice;
                echo $itemsoldCount;
}

1、形如

echo $itemUrl;echo $itemPicUrl;echo $itemTitle;echo $itemPrice;echo $itemsoldCount;
的可简化为
show($itemUrl, $itemPicUrl, $itemTitle, $itemPrice, $itemsoldCount);function show() {  echo join('', func_get_args());}
函数 show 中可做各种美化工作

2、原式就可写作
if($_MODULE[huoqubaobei]=="2"){$arr = explode(',',$_MODULE[item_1]);	 foreach($arr as $value){		$item=$itemManager-> queryById ($value);		show($uriManager->detailURI($item),			$item->getPicUrl(160),			$item->title,			$item->price,			$item->soldCount			);	}}else if($_MODULE[huoqubaobei]=="1"){	$items=$itemManager->queryByKeyword("","hotsell",9);	foreach($items as $item ){		show($uriManager->detailURI($item),			$item->getPicUrl(160),			$item->title,			$item->price,			$item->soldCount			);	}}else if($_MODULE[huoqubaobei]=="3"){	$items = $itemManager->queryByCategory($categoryId,"hotsell",10);	foreach($items as $item){		show($uriManager->detailURI($item),			$item->getPicUrl(160),			$item->title,			$item->price,			$item->soldCount			);	}}else{	show("#",		"assets/images/nvtp.jpg",		"请添加宝贝描述请添加宝贝描述请添加宝贝描述请",		628,		89,		);}


3、考虑到前三个分支存在相同的取数方法,于是可以再写一个函数
function item_show($items, $uriManager) {	foreach($items as $item){		show($uriManager->detailURI($item),			$item->getPicUrl(160),			$item->title,			$item->price,			$item->soldCount			);	}}


4、代码就进一步变为
if($_MODULE[huoqubaobei]=="2"){$arr = explode(',',$_MODULE[item_1]);	 foreach($arr as $value){		items_show(array($itemManager->queryById($value)), $uriManager);	}}else if($_MODULE[huoqubaobei]=="1"){	items_show($itemManager->queryByKeyword("","hotsell",9), , $uriManager);}else if($_MODULE[huoqubaobei]=="3"){	items_show($itemManager->queryByCategory($categoryId,"hotsell",10), $uriManager);}else{	show("#",		"assets/images/nvtp.jpg",		"请添加宝贝描述请添加宝贝描述请添加宝贝描述请",		628,		89,		);}function show() {	echo join('', func_get_args());}function items_show($items, $uriManager) {	foreach($items as $item){		show($uriManager->detailURI($item),			$item->getPicUrl(160),			$item->title,			$item->price,			$item->soldCount			);	}}


这段才是最省事的!

还可以这样写~  


你在每个分支里输出同样的一组变量?
为何不在条件结构出来后在输出? 不明白你的意思。。。

把那些变量的地方都放到外面来

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn