2014-02-14 39 views
0

我正在製作一個表單,用戶可以在其中建立一個購物車,然後一旦他們提交,他們將被帶到一個頁面,顯示他們的購物車中的最終內容。在幕後,PHP正在向我發送一封電子郵件,它需要包含已經在頁面上呈現的完全相同的表格,但是我不想再次重新構建表格,我想使用已經存在的表格建成。我不知道該怎麼做。我的代碼的作品,一切都是我想要的,所以不要再次顯示錶格沒有重新處理所有PHP

下面是一個代碼樣我喜歡的樣本,我剪掉了重複的東西,使它更容易看。

<table align="center" width="100%"> 
    <thead align="center"> 
    <tr> 
    <td style="border-color: #000000; background-color: #000000; color: #ffffff; border-style: solid; border-width: 1px;" class="head"><b>Product</b></td> 
    <td style="border-color: #000000; background-color: #000000; color: #ffffff; border-style: solid; border-width: 1px;" class="head"><b>Item ID</b></td> 
    <td style="border-color: #000000; background-color: #000000; color: #ffffff; border-style: solid; border-width: 1px;" class="head"><b>Quantity</b></td> 
    <td style="border-color: #000000; background-color: #000000; color: #ffffff; border-style: solid; border-width: 1px;" class="head"><b>Cost</b></td> 
    </tr> 
    </thead> 
    <tbody> 
    <?php 
    $i = 0; 
    $cmf073 = mysqli_result($result, $i, "cmf073"); 
    $cmf073total = mysqli_result($result, $i, "cmf073total"); 
    if($cmf073 > 0) 
    {?> 
    <tr> 
    <td align="center" style="border-color: #000000; background-color: #ffffff; border-style: solid; border-width: 1px;" class="body"> 
    Printable 3-Up Certified Mail® Form 
    </td> 
    <td align="center" style="border-color: #000000; background-color: #ffffff; border-style: solid; border-width: 1px;" class="body"> 
    CMF-073 
    </td> 
    <td align="center" style="border-color: #000000; background-color: #ffffff; border-style: solid; border-width: 1px;" class="body"> 
    <?php echo number_format($cmf073 * 1000, 0) ." Forms";?> 
    </td> 
    <td align="center" style="border-color: #000000; background-color: #ffffff; border-style: solid; border-width: 1px;" class="body"> 
    $<?php echo number_format($cmf073total, 2);?> 
    </td></tr> 
    <?php 
    } 
    echo "<tr border='0'><td></td><td></td><td></td><td align='center'><b>Cost Estimate: "; 
    echo "$".number_format($total, 2)."</b><br /> 
    <font style='font-size: 8px;'>Plus Tax, Shipping and Handling</font></td></tr></tbody></table>"; 

    } 
    $email = "[email protected]"; 
    $subject = "New Customer Order Request from $company" ; 
    $message = "A new order request has been submitted on y.net</br></br> 

    Contact Info:</br> 
    $firstName $lastName</br> 
    $phone | $email</br> 
    $company</br> 
    $address1</br> 
    $address2</br> 
    $city, $state $zip"; 
    $headers = 'From: [email protected]' . "\r\n" . 
    'Reply-To: [email protected]' . "\r\n" . 
    'Content-type: text/html; charset=utf-8' . "\r\n" . 
    'X-Mailer: PHP/' . phpversion(); 
    mail($email, $subject, $message, $headers); 

所以我需要在代碼頂部已經生成的表格放在電子郵件$ message的底部。但是我不想再重新生成一切,因爲PHP已經創建了我需要的表。對不起,如果這是令人困惑的,我真的不知道如何解釋它。謝謝。

+0

也許讓一個變量叫'$ table'這包含所有行等即'$表='

​​​​​​
'。然後將其回顯到頁面上,並以類似的方式將其回傳到電子郵件中。 –

+0

像smarty這樣的模板引擎可以緩存你的模板。 –

回答

2

在餐桌上

<?php 
ob_start(); 
?> 
<table align='.....' 
//stuff 
//more stuff 

年初在餐桌上

</table> 
<?php 
$myTable=ob_get_contents(); 
ob_end_clean(); 
echo $myTable; 
?> 

結束現在$myTable你有表的內容,你可以在你的郵件中使用它。

$message.=$myTable; 
mail($email, $subject, $message, $headers); 
+0

謝謝!希望我能接受所有的答案,但你會得到最詳細的檢查。 – RugerSR9

+0

很高興能幫到您! – ojovirtual

+0

+1的細節。 –

4

您可以使用輸出緩衝:

<?php ob_start(); ?>

... // your table html exactly as you have it

<?php $var = ob_get_clean();

在這一點上,你可以重複$var您的頁面,也包括它在一個mail()呼叫,您避免必須通過鏈接PHP字符串將其全部存儲在變量中。

+0

令人驚歎!甚至不知道這是我自己存在的。 +1是第一個提供此解決方案的人。 –

1

而不是使用<?..? >標籤直接寫入頁,表的內容存儲在一個變量:

if($cmf073 > 0) 
    $table=" 
    <tr> 
    <td align="center" style="border-color: #000000; background-color: #ffffff; border-style: solid; border-width: 1px;" class="body"> 
    Printable 3-Up Certified Mail® Form 
    </td> 
    <td align="center" style="border-color: #000000; background-color: #ffffff; border-style: solid; border-width: 1px;" class="body"> 
    CMF-073 
    </td> 
    <td align="center" style="border-color: #000000; background-color: #ffffff; border-style: solid; border-width: 1px;" class="body"> 
    ".number_format($cmf073 * 1000, 0)." Forms 
    </td> 
    <td align="center" style="border-color: #000000; background-color: #ffffff; border-style: solid; border-width: 1px;" class="body"> 
    \$".number_format($cmf073total, 2)." 
    </td></tr> 
    "; 
} 

然後,你可以說

print($table); 

和發送電子郵件時,您還可以再次使用該變量爲郵件內容:

$message = "A new order request has been submitted on y.net</br></br> 

$table 

Contact Info:</br> 
$firstName $lastName</br>