How to Abbreviate code

Source: Internet
Author: User
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 = "Please add baby description Please add baby description Please add baby description please";                $itemPrice = 628; $itemsoldCount = 89; echo $itemUrl; echo $itemPicUrl; echo $itemTitle; echo $itemPrice; echo $itemsoldCount;}


How to abbreviate?
Echo $itemPicUrl;
Echo $itemTitle;
Echo $itemPrice;
Echo $itemsoldCount;
Why don't you let them repeat the writing?

I was going to add it to the array and loop it, but it doesn't work. Who has a better way




Reply to discussion (solution)

Do you output the same set of variables in each branch?
Why not after the conditional structure comes out in the output?

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 ();    $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 = "Please add baby description Please add baby description Please add baby description please"; $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 ();    $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 = "Please add baby description Please add baby description Please add baby description please"; $itemPrice = 628; $itemsoldCount = 89; echo $itemUrl; echo $itemPicUrl; echo $itemTitle; echo $itemPrice; echo $itemsoldCount;}


Haha, you're still repeating the last side.

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 ();    $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 = "Please add baby description Please add baby description Please add baby description please"; $itemPrice = 628; $itemsoldCount = 89; echo $itemUrl; echo $itemPicUrl; echo $itemTitle; echo $itemPrice; echo $itemsoldCount;}

And I was in the back of the else actually ElseIf ($_module[huoqubaobei]== "4")

Do you output the same set of variables in each branch?
Why not after the conditional structure comes out in the output? I don't know what you mean.


Do you output the same set of variables in each branch?
Why not after the conditional structure comes out in the output? I don't know what you mean.

It means, you see, every if else.
Echo $itemUrl;
Echo $itemPicUrl;
Echo $itemTitle;
Echo $itemPrice;
Echo $itemsoldCount;
Can be added in the beam position, do not need to be in each? 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",;d efault: $items = Array ();} if ($items) {foreach ($items as $item) {$itemUrl = $uriManager->detailuri ($item); $itemPicUrl = $item->getpicurl ( $itemTitle = $item->title; $itemPrice = $item->price; $itemsoldCount = $item->soldcount; $response. = $itemUrl. $itemPicUrl. $itemTitle. $itemPrice. $itemsoldCount}}else{$itemUrl = "#"; $itemPicUrl = "assets/ Images/nvtp.jpg "; $itemTitle =" Please add baby description Please add baby description Please add baby description please "; $itemPrice = 628; $itemsoldCount =89; $response. = $itemUrl. $ Itempicurl. $itemTitle. $itemPrice. $itemsoldCount}echo $response;

Modified a bit,?? Just?

$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 ( $itemTitle = $item->title; $itemPrice = $item->price; $itemsoldCount = $item->soldcount; $response. = $itemUrl. $itemPicUrl. $itemTitle. $itemPrice. $itemsoldCount}}else{$itemUrl = "#"; $itemPicUrl = "assets/ Images/nvtp.jpg "; $itemTitle =" Please add baby description Please add baby description Please add baby description please "; $itemPrice = 628; $itemsoldCount =89; $response. = $itemUrl. $ Itempicurl. $itemTitle. $itemPrice. $itemsoldCount}echo $response;

This code should actually be written into classes and inherit $itemmanager classes (or constituent object chains) would be better

1, Shape as

echo $itemUrl; echo $itemPicUrl; echo $itemTitle; echo $itemPrice; Echo $itemsoldCount;
can be simplified to
Show ($itemUrl, $itemPicUrl, $itemTitle, $itemPrice, $itemsoldCount); function Show () {  echo join (', Func_get_args ( ));}
Function Show can do a variety of landscaping work

2, the original type can be written
if ($_module[huoqubaobei]== "2") {$arr = explode (', ', ' $_module[item_1]); foreach ($arr as $value) {$item = $itemManager- > Querybyid ($value); Show ($uriManager->detailuri ($item), $item->getpicurl, $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, $item->title, $item->price, $item- >soldcount);}} else if ($_module[huoqubaobei]== "3") {$items = $itemManager->querybycategory ($categoryId, "Hotsell", "ten"); foreach ($ Items as $item) {Show ($uriManager->detailuri ($item), $item->getpicurl, $item->title, $item->price,$ Item->soldcount);}} Else{show ("#", "assets/images/nvtp.jpg", "Please add baby description Please add baby description Please add baby description please", 628, 89,);}


3, considering that the first three branches have the same number of methods, so you can write a function
function Item_show ($items, $uriManager) {foreach ($items as $item) {Show ($uriManager->detailuri ($item), $item- Getpicurl, $item->title, $item->price, $item->soldcount);}}


4, the code is further changed to
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", "Please add baby description Please add baby description Please add baby description please", 628, 89,);} Function Show () {echo join (' ', Func_get_args ());} function Items_show ($items, $uriManager) {foreach ($items as $item) {Show ($uriManager->detailuri ($item), $item- Getpicurl, $item->title, $item->price, $item->soldcount);}}

Haha, the last floor is right.

Modified a bit,?? Just?

$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 ( $itemTitle = $item->title; $itemPrice = $item->price; $itemsoldCount = $item->soldcount; $response. = $itemUrl. $itemPicUrl. $itemTitle. $itemPrice. $itemsoldCount}}else{$itemUrl = "#"; $itemPicUrl = "assets/ Images/nvtp.jpg "; $itemTitle =" Please add baby description Please add baby description Please add baby description please "; $itemPrice = 628; $itemsoldCount =89; $response. = $itemUrl. $ Itempicurl. $itemTitle. $itemPrice. $itemsoldCount}echo $response;


$ITEMURL = "#";
$ITEMPICURL = "Assets/images/nvtp.jpg";
$itemTitle = "Please add baby description Please add baby description Please add baby description please";
$itemPrice = 628;
$itemsoldCount = 89;
$response. = $itemUrl. $itemPicUrl. $itemTitle. $itemPrice. $itemsoldCount
How to write on the loop

The last love is not to follow.
If we have to follow through, we need to provide a follow-up.

The last love is not to follow.
If we have to follow through, we need to provide a follow-up.

foreach ($items as $item) {times like him

The last one, your items are, how to follow?

The last one, your items are, how to follow?

In fact, if the judgment in front of him ($items) is wrong, it cannot be judged.

I was at the back of the else if ($_module[huoqubaobei]== "4") judged by

I didn't see anything to circulate.
Else
{
$ITEMURL = "#";
$ITEMPICURL = "Assets/images/nvtp.jpg";
$itemTitle = "Please add baby description Please add baby description Please add baby description please";
$itemPrice = 628;
$itemsoldCount = 89;
Echo $itemUrl;
Echo $itemPicUrl;
Echo $itemTitle;
Echo $itemPrice;
Echo $itemsoldCount;
}

1, Shape as

echo $itemUrl; echo $itemPicUrl; echo $itemTitle; echo $itemPrice; Echo $itemsoldCount;
can be simplified to
Show ($itemUrl, $itemPicUrl, $itemTitle, $itemPrice, $itemsoldCount); function Show () {  echo join (', Func_get_args ( ));}
Function Show can do a variety of landscaping work

2, the original type can be written
if ($_module[huoqubaobei]== "2") {$arr = explode (', ', ' $_module[item_1]); foreach ($arr as $value) {$item = $itemManager- > Querybyid ($value); Show ($uriManager->detailuri ($item), $item->getpicurl, $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, $item->title, $item->price, $item- >soldcount);}} else if ($_module[huoqubaobei]== "3") {$items = $itemManager->querybycategory ($categoryId, "Hotsell", "ten"); foreach ($ Items as $item) {Show ($uriManager->detailuri ($item), $item->getpicurl, $item->title, $item->price,$ Item->soldcount);}} Else{show ("#", "assets/images/nvtp.jpg", "Please add baby description Please add baby description Please add baby description please", 628, 89,);}


3, considering that the first three branches have the same number of methods, so you can write a function
function Item_show ($items, $uriManager) {foreach ($items as $item) {Show ($uriManager->detailuri ($item), $item- Getpicurl, $item->title, $item->price, $item->soldcount);}}


4, the code is further changed to
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", "Please add baby description Please add baby description Please add baby description please", 628, 89,);} Function Show () {echo join (' ', Func_get_args ());} function Items_show ($items, $uriManager) {foreach ($items as $item) {Show ($uriManager->detailuri ($item), $item- Getpicurl, $item->title, $item->price, $item->soldcount);}}


This paragraph is the most convenient!

can also write ~


Do you output the same set of variables in each branch?
Why not after the conditional structure comes out in the output? I don't know what you mean.

Put all those variables out of the place.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.