Maison >développement back-end >tutoriel php >怎么简写代码
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;}
你在每个分支里输出同样的一组变量?
为何不在条件结构出来后在输出?
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;}
你在每个分支里输出同样的一组变量?
为何不在条件结构出来后在输出? 不明白你的意思。。。
你在每个分支里输出同样的一组变量?
为何不在条件结构出来后在输出? 不明白你的意思。。。
意思就是,你看每?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 中可做各种美化工作
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, );}
function item_show($items, $uriManager) { foreach($items as $item){ show($uriManager->detailURI($item), $item->getPicUrl(160), $item->title, $item->price, $item->soldCount ); }}
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;
最後一?情?不是不用循???
如果要循?,需要提供循?的次?。
最後一?情?不是不用循???
如果要循?,需要提供循?的次?。
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 中可做各种美化工作
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, );}
function item_show($items, $uriManager) { foreach($items as $item){ show($uriManager->detailURI($item), $item->getPicUrl(160), $item->title, $item->price, $item->soldCount ); }}
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 ); }}
还可以这样写~
你在每个分支里输出同样的一组变量?
为何不在条件结构出来后在输出? 不明白你的意思。。。
把那些变量的地方都放到外面来