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 ); }}
这段才是最省事的!
还可以这样写~
你在每个分支里输出同样的一组变量?
为何不在条件结构出来后在输出? 不明白你的意思。。。
把那些变量的地方都放到外面来

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

In this article, we're going to explore the notification system in the Laravel web framework. The notification system in Laravel allows you to send notifications to users over different channels. Today, we'll discuss how you can send notifications ov

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
