将静态PHP代码更改为循环以节省时间和速度

将静态PHP代码更改为循环以节省时间和速度,php,function,loops,if-statement,Php,Function,Loops,If Statement,我有一个商店代码,我用它来存储数据并通过电子邮件发送给我制作的购物者。 它工作正常,功能正常 问题是,我没有循环编码它,因为我不知道怎么做 我目前有5个产品的限制,即使现在我也很难编辑任何东西,因为我必须一个接一个地将更改应用到5个部分 如果你能帮我把这个改成一个循环,这将是一个救命稻草,我可以把我的产品限制改成无限而不是5,并且可以快速编辑布局。 感谢您的帮助 if ($productname1 != "") { $message .= "<tr style='background: #

我有一个商店代码,我用它来存储数据并通过电子邮件发送给我制作的购物者。 它工作正常,功能正常

问题是,我没有循环编码它,因为我不知道怎么做

我目前有5个产品的限制,即使现在我也很难编辑任何东西,因为我必须一个接一个地将更改应用到5个部分

如果你能帮我把这个改成一个循环,这将是一个救命稻草,我可以把我的产品限制改成无限而不是5,并且可以快速编辑布局。 感谢您的帮助

if ($productname1 != "") {
$message .= "<tr style='background: #F30'><td colspan='2' style='font-size: 24px;text-align: center;'><strong>Product Number 1</strong> </td></tr>";
$message .= "<tr style='background: #CC0000;'><td><strong>Product Name</strong> </td><td style='direction:ltr;text-align:right'><strong>" . $productname1 . "</strong></td></tr>";
$message .= "<tr style='background: #09C;'><td><strong>Link</strong> </td><td style='font-size:12px;direction:ltr;text-align:center'><strong><a href='" . $link1 . "' style='text-decoration:none;color:#FFF;'>" . $link1 . "</a></strong></td></tr>";
$message .= "<tr style='background: #CC0000;'><td><strong>Color</strong> </td><td style='direction:ltr;text-align:right'><strong>" . $color1 . "</strong></td></tr>";
$message .= "<tr style='background: #09C;'><td><strong>Size</strong> </td><td><strong>" . $size1 . "</strong></td></tr>";
$message .= "<tr style='background: #CC0000;'><td><strong>Model</strong> </td><td><strong>" . $otherinfo1 . "</strong></td></tr>";
$message .= "<tr style='background: #09C;'><td><strong>Quantity</strong> </td><td style='direction:ltr;text-align:right'><strong>" . $no1 . "</strong></td></tr>";
$message .= "<tr style='background: #CC0000;'><td><strong>Description</strong> </td><td><strong>" . $other1 . "</strong></td></tr>";
$message .= "<tr style='background: #09C;'><td><strong>Price</strong></td><td><div style='float:left;width:90%;text-align:right;'><strong>$</strong></div><strong>00.00</strong></td></tr>";
$message .= "<tr style='background: #CC0000;'><td><strong>Domestic Shipping</strong></td><td><div style='float:left;width:90%;text-align:right;'><strong>$</strong></div><strong>00.00</strong></td></tr>";
if ($delivery == "Extra handling"){
$message .= "<tr style='background: #09C;'><td><strong>Extra handling fee</strong></td><td><div style='float:left;width:90%;text-align:right;'><strong>$</strong></div><strong>00.00</strong></td></tr>";
}
else if ($delivery == "No handling fees"){
$message .= "<tr style='background: #09C;'><td><strong>No handling fees to  ".$clientcity."</strong></td><td><div style='float:left;width:90%;text-align:right;'><strong>$</strong></div><strong>00.00</strong></td></tr>";
}
}
if ($productname2 != "") {
$message .= "<tr style='background: #F30'><td colspan='2' style='font-size: 24px;text-align: center;'><strong>Product Number 2</strong> </td></tr>";
$message .= "<tr style='background: #CC0000;'><td><strong>Product Name</strong> </td><td style='direction:ltr;text-align:right'><strong>" . $productname2 . "</strong></td></tr>";
$message .= "<tr style='background: #09C;'><td><strong>Link</strong> </td><td style='font-size:12px;direction:ltr;text-align:center'><strong><a href='" . $link2 . "' style='text-decoration:none;color:#FFF;'>" . $link2 . "</a></strong></td></tr>";
$message .= "<tr style='background: #CC0000;'><td><strong>Color</strong> </td><td style='direction:ltr;text-align:right'><strong>" . $color2 . "</strong></td></tr>";
$message .= "<tr style='background: #09C;'><td><strong>Size</strong> </td><td><strong>" . $size2 . "</strong></td></tr>";
$message .= "<tr style='background: #CC0000;'><td><strong>Model</strong> </td><td><strong>" . $otherinfo2 . "</strong></td></tr>";
$message .= "<tr style='background: #09C;'><td><strong>Quantity</strong> </td><td style='direction:ltr;text-align:right'><strong>" . $no2 . "</strong></td></tr>";
$message .= "<tr style='background: #CC0000;'><td><strong>Description</strong> </td><td><strong>" . $other2 . "</strong></td></tr>";
$message .= "<tr style='background: #09C;'><td><strong>Price</strong></td><td><div style='float:left;width:90%;text-align:right;'><strong>$</strong></div><strong>00.00</strong></td></tr>";
$message .= "<tr style='background: #CC0000;'><td><strong>Domestic Shipping</strong></td><td><div style='float:left;width:90%;text-align:right;'><strong>$</strong></div><strong>00.00</strong></td></tr>";
if ($delivery == "Extra handling"){
$message .= "<tr style='background: #09C;'><td><strong>Extra handling fee</strong></td><td><div style='float:left;width:90%;text-align:right;'><strong>$</strong></div><strong>00.00</strong></td></tr>";
}
else if ($delivery == "No handling fees"){
$message .= "<tr style='background: #09C;'><td><strong>No handling fees to  ".$clientcity."</strong></td><td><div style='float:left;width:90%;text-align:right;'><strong>$</strong></div><strong>00.00</strong></td></tr>";
}
}
if ($productname3 != "") {
$message .= "<tr style='background: #F30'><td colspan='2' style='font-size: 24px;text-align: center;'><strong>Product Number 3</strong> </td></tr>";
$message .= "<tr style='background: #CC0000;'><td><strong>Product Name</strong> </td><td style='direction:ltr;text-align:right'><strong>" . $productname3 . "</strong></td></tr>";
$message .= "<tr style='background: #09C;'><td><strong>Link</strong> </td><td style='font-size:12px;direction:ltr;text-align:center'><strong><a href='" . $link3 . "' style='text-decoration:none;color:#FFF;'>" . $link3 . "</a></strong></td></tr>";
$message .= "<tr style='background: #CC0000;'><td><strong>Color</strong> </td><td style='direction:ltr;text-align:right'><strong>" . $color3 . "</strong></td></tr>";
$message .= "<tr style='background: #09C;'><td><strong>Size</strong> </td><td><strong>" . $size3 . "</strong></td></tr>";
$message .= "<tr style='background: #CC0000;'><td><strong>Model</strong> </td><td><strong>" . $otherinfo3 . "</strong></td></tr>";
$message .= "<tr style='background: #09C;'><td><strong>Quantity</strong> </td><td style='direction:ltr;text-align:right'><strong>" . $no3 . "</strong></td></tr>";
$message .= "<tr style='background: #CC0000;'><td><strong>Description</strong> </td><td><strong>" . $other3 . "</strong></td></tr>";
$message .= "<tr style='background: #09C;'><td><strong>Price</strong></td><td><div style='float:left;width:90%;text-align:right;'><strong>$</strong></div><strong>00.00</strong></td></tr>";
$message .= "<tr style='background: #CC0000;'><td><strong>Domestic Shipping</strong></td><td><div style='float:left;width:90%;text-align:right;'><strong>$</strong></div><strong>00.00</strong></td></tr>";
if ($delivery == "Extra handling"){
$message .= "<tr style='background: #09C;'><td><strong>Extra handling fee</strong></td><td><div style='float:left;width:90%;text-align:right;'><strong>$</strong></div><strong>00.00</strong></td></tr>";
}
else if ($delivery == "No handling fees"){
$message .= "<tr style='background: #09C;'><td><strong>No handling fees to  ".$clientcity."</strong></td><td><div style='float:left;width:90%;text-align:right;'><strong>$</strong></div><strong>00.00</strong></td></tr>";
}
}
if ($productname4 != "") {
$message .= "<tr style='background: #F30'><td colspan='2' style='font-size: 24px;text-align: center;'><strong>Product Number 4</strong> </td></tr>";
$message .= "<tr style='background: #CC0000;'><td><strong>Product Name</strong> </td><td style='direction:ltr;text-align:right'><strong>" . $productname4 . "</strong></td></tr>";
$message .= "<tr style='background: #09C;'><td><strong>Link</strong> </td><td style='font-size:12px;direction:ltr;text-align:center'><strong><a href='" . $link4 . "' style='text-decoration:none;color:#FFF;'>" . $link4 . "</a></strong></td></tr>";
$message .= "<tr style='background: #CC0000;'><td><strong>Color</strong> </td><td style='direction:ltr;text-align:right'><strong>" . $color4 . "</strong></td></tr>";
$message .= "<tr style='background: #09C;'><td><strong>Size</strong> </td><td><strong>" . $size4 . "</strong></td></tr>";
$message .= "<tr style='background: #CC0000;'><td><strong>Model</strong> </td><td><strong>" . $otherinfo4 . "</strong></td></tr>";
$message .= "<tr style='background: #09C;'><td><strong>Quantity</strong> </td><td style='direction:ltr;text-align:right'><strong>" . $no4 . "</strong></td></tr>";
$message .= "<tr style='background: #CC0000;'><td><strong>Description</strong> </td><td><strong>" . $other4 . "</strong></td></tr>";
$message .= "<tr style='background: #09C;'><td><strong>Price</strong></td><td><div style='float:left;width:90%;text-align:right;'><strong>$</strong></div><strong>00.00</strong></td></tr>";
$message .= "<tr style='background: #CC0000;'><td><strong>Domestic Shipping</strong></td><td><div style='float:left;width:90%;text-align:right;'><strong>$</strong></div><strong>00.00</strong></td></tr>";
if ($delivery == "Extra handling"){
$message .= "<tr style='background: #09C;'><td><strong>Extra handling fee</strong></td><td><div style='float:left;width:90%;text-align:right;'><strong>$</strong></div><strong>00.00</strong></td></tr>";
}
else if ($delivery == "No handling fees"){
$message .= "<tr style='background: #09C;'><td><strong>No handling fees to  ".$clientcity."</strong></td><td><div style='float:left;width:90%;text-align:right;'><strong>$</strong></div><strong>00.00</strong></td></tr>";
}
}
if ($productname5 != "") {
$message .= "<tr style='background: #F30'><td colspan='2' style='font-size: 24px;text-align: center;'><strong>Product Number 5</strong> </td></tr>";
$message .= "<tr style='background: #CC0000;'><td><strong>Product Name</strong> </td><td style='direction:ltr;text-align:right'><strong>" . $productname5 . "</strong></td></tr>";
$message .= "<tr style='background: #09C;'><td><strong>Link</strong> </td><td style='font-size:12px;direction:ltr;text-align:center'><strong><a href='" . $link5 . "' style='text-decoration:none;color:#FFF;'>" . $link5 . "</a></strong></td></tr>";
$message .= "<tr style='background: #CC0000;'><td><strong>Color</strong> </td><td style='direction:ltr;text-align:right'><strong>" . $color5 . "</strong></td></tr>";
$message .= "<tr style='background: #09C;'><td><strong>Size</strong> </td><td><strong>" . $size5 . "</strong></td></tr>";
$message .= "<tr style='background: #CC0000;'><td><strong>Model</strong> </td><td><strong>" . $otherinfo5 . "</strong></td></tr>";
$message .= "<tr style='background: #09C;'><td><strong>Quantity</strong> </td><td style='direction:ltr;text-align:right'><strong>" . $no5 . "</strong></td></tr>";
$message .= "<tr style='background: #CC0000;'><td><strong>Description</strong> </td><td><strong>" . $other5 . "</strong></td></tr>";
$message .= "<tr style='background: #09C;'><td><strong>Price</strong></td><td><div style='float:left;width:90%;text-align:right;'><strong>$</strong></div><strong>00.00</strong></td></tr>";
$message .= "<tr style='background: #CC0000;'><td><strong>Domestic Shipping</strong></td><td><div style='float:left;width:90%;text-align:right;'><strong>$</strong></div><strong>00.00</strong></td></tr>";
if ($delivery == "Extra handling"){
$message .= "<tr style='background: #09C;'><td><strong>Extra handling fee</strong></td><td><div style='float:left;width:90%;text-align:right;'><strong>$</strong></div><strong>00.00</strong></td></tr>";
}
else if ($delivery == "No handling fees"){
$message .= "<tr style='background: #09C;'><td><strong>No handling fees to  ".$clientcity."</strong></td><td><div style='float:left;width:90%;text-align:right;'><strong>$</strong></div><strong>00.00</strong></td></tr>";
}
如果($PRODUCTNAME!=“”){
$message.=“产品编号1”;
$message.=“产品名称”“$PRODUCTNAME。””;
$message.=“链接””;
$message.=“颜色”“$color1.””;
$message.=“大小”“$size1.””;
$message.=“模型”“$otherinfo1.””;
$message.=“数量”“$no1.””;
$message.=“说明”“$other1.””;
$message.=“价格$00.00”;
$message.=“国内航运$00.00”;
如果($delivery==“额外处理”){
$message.=“额外手续费$00.00”;
}
否则,如果($delivery==“无手续费”){
$message.=“无需向“$clientcity”收取手续费。$00.00”;
}
}
如果($PRODUCTNAME 2!=“”){
$message.=“产品编号2”;
$message.=“产品名称”“$PRODUCTNAME 2.””;
$message.=“链接””;
$message.=“颜色”“$color2.””;
$message.=“大小”“$size2.””;
$message.=“模型”“$otherinfo2.””;
$message.=“数量”“$no2.””;
$message.=“说明”“$other2.””;
$message.=“价格$00.00”;
$message.=“国内航运$00.00”;
如果($delivery==“额外处理”){
$message.=“额外手续费$00.00”;
}
否则,如果($delivery==“无手续费”){
$message.=“无需向“$clientcity”收取手续费。$00.00”;
}
}
如果($PRODUCTNAME!=“”){
$message.=“产品编号3”;
$message.=“产品名称”“$PRODUCTNAME 3.””;
$message.=“链接””;
$message.=“颜色”“$color3.””;
$message.=“大小”“$size3.””;
$message.=“型号”“$otherinfo3.””;
$message.=“数量”“$no3.””;
$message.=“说明”“$other3.””;
$message.=“价格$00.00”;
$message.=“国内航运$00.00”;
如果($delivery==“额外处理”){
$message.=“额外手续费$00.00”;
}
否则,如果($delivery==“无手续费”){
$message.=“无需向“$clientcity”收取手续费。$00.00”;
}
}
如果($PRODUCTNAME!=“”){
$message.=“产品编号4”;
$message.=“产品名称”“$PRODUCTNAME.”“;
$message.=“链接””;
$message.=“颜色”“$color4.””;
$message.=“大小”“$size4.””;
$message.=“模型”“$otherinfo4.””;
$message.=“数量”“$no4.””;
$message.=“说明”“$other4.””;
$message.=“价格$00.00”;
$message.=“国内航运$00.00”;
如果($delivery==“额外处理”){
$message.=“额外手续费$00.00”;
}
否则,如果($delivery==“无手续费”){
$message.=“无需向“$clientcity”收取手续费。$00.00”;
}
}
如果($PRODUCTNAME!=“”){
$message.=“产品编号5”;
$message.=“产品名称”“$PRODUCTNAME。””;
$message.=“链接””;
$message.=“颜色”“$color5.””;
$message.=“大小”“$size5.””;
$message.=“模型”“$otherinfo5.””;
$message.=“数量”“$no5.””;
$message.=“说明”“$other5.””;
$message.=“价格$00.00”;
$message.=“国内航运$00.00”;
如果($delivery==“额外处理”){
$message.=“额外手续费$00.00”;
}
否则,如果($delivery==“无手续费”){
$message.=“无需向“$clientcity”收取手续费。$00.00”;
}

有一种方法可以做到这一点:

而不是写作,例如:

if ($productname5 != "") {
写:

if (${"productname" . $i} != "") {
并有一个循环,
$i
每次添加一个。如果需要帮助,请告诉我

$message = '';

$products = array(
    0 => array( 
        'productname' => $productname1,
        'link' => $link1,
        'color' => $color1,
        'size' => $size1,
        'otherinfo' => $otherinfo1,
        'no' => $no1,
        'other' => $other1
    ),

    1 => array( 
        'productname' => $productname2,
        'link' => $link2,
        'color' => $color2,
        'size' => $size2,
        'otherinfo' => $otherinfo2,
        'no' => $no2,
        'other' => $other2
    ),

    2 => array( 
        'productname' => $productname3,
        'link' => $link3,
        'color' => $color3,
        'size' => $size3,
        'otherinfo' => $otherinfo3,
        'no' => $no3,
        'other' => $other3
    ),

    3 => array( 
        'productname' => $productname4,
        'link' => $link4,
        'color' => $color4,
        'size' => $size4,
        'otherinfo' => $otherinfo4,
        'no' => $no4,
        'other' => $other4
    ),

    4 => array( 
        'productname' => $productname5,
        'link' => $link5,
        'color' => $color5,
        'size' => $size5,
        'otherinfo' => $otherinfo5,
        'no' => $no5,
        'other' => $other5
    ),
);

foreach ($products as $i => $product) {
    $message .= "<tr style='background: #F30'><td colspan='2' style='font-size: 24px;text-align: center;'><strong>Product Number " . ($i +1) . "</strong> </td></tr>";
    $message .= "<tr style='background: #CC0000;'><td><strong>Product Name</strong> </td><td style='direction:ltr;text-align:right'><strong>" . $product['productname'] . "</strong></td></tr>";
    $message .= "<tr style='background: #09C;'><td><strong>Link</strong> </td><td style='font-size:12px;direction:ltr;text-align:center'><strong><a href='" . $product['link'] . "' style='text-decoration:none;color:#FFF;'>" . $product['link'] . "</a></strong></td></tr>";
    $message .= "<tr style='background: #CC0000;'><td><strong>Color</strong> </td><td style='direction:ltr;text-align:right'><strong>" . $product['color'] . "</strong></td></tr>";

    $message .= "<tr style='background: #09C;'><td><strong>Size</strong> </td><td><strong>" . $product['size'] . "</strong></td></tr>";
    $message .= "<tr style='background: #CC0000;'><td><strong>Model</strong> </td><td><strong>" . $product['otherinfo'] . "</strong></td></tr>";
    $message .= "<tr style='background: #09C;'><td><strong>Quantity</strong> </td><td style='direction:ltr;text-align:right'><strong>" . $product['no'] . "</strong></td></tr>";
    $message .= "<tr style='background: #CC0000;'><td><strong>Description</strong> </td><td><strong>" . $product['other'] . "</strong></td></tr>";
    $message .= "<tr style='background: #09C;'><td><strong>Price</strong></td><td><div style='float:left;width:90%;text-align:right;'><strong>$</strong></div><strong>00.00</strong></td></tr>";
    $message .= "<tr style='background: #CC0000;'><td><strong>Domestic Shipping</strong></td><td><div style='float:left;width:90%;text-align:right;'><strong>$</strong></div><strong>00.00</strong></td></tr>";
    if ($delivery == "Extra handling"){
    $message .= "<tr style='background: #09C;'><td><strong>Extra handling fee</strong></td><td><div style='float:left;width:90%;text-align:right;'><strong>$</strong></div><strong>00.00</strong></td></tr>";
    }
    else if ($delivery == "No handling fees"){
    $message .= "<tr style='background: #09C;'><td><strong>No handling fees to  ".$clientcity."</strong></td><td><div style='float:left;width:90%;text-align:right;'><strong>$</strong></div><strong>00.00</strong></td></tr>";
    }
}