Question

I am trying to create a variable, $body, with a foreach statement inside of it. I am passing the variable into a mail function. The $body variable's foreach statement is supposed to display a user's shopping cart data. The reason I am putting the $body variable into a mail function is to send me an email of the user's shopping cart products, so I can see what they have ordered from my website. So far, the code is not working in Dreamweaver and is highlighting the code in red, meaning that the code is broken in some way. I cannot figure out the problem.

This is my mail function's code:

mail($to, $subject, $body, $headers);

This is the $body variable I am using for the foreach statement:

$body = '   if(isset($_SESSION["products"]))
     {
        $total = 0;
        echo '<form method="post" action="PAYMENT-GATEWAY">';
        echo '<ul>';
        $cart_items = 0;
        foreach ($_SESSION["products"] as $cart_itm)
        {
           $product_code = $cart_itm["code"];
           $queryy = "SELECT TOP 1 product_name,product_desc, price FROM products WHERE product_code='$product_code'";
           $results = mssql_query($queryy, $mysqli);
           $obj = mssql_fetch_object($results);

            echo '<li class="cart-itm">';
            echo '<span class="remove-itm"><a href="cart_update.php?removep='.$cart_itm["code"].'&return_url='.$current_url.'">&times;</a></span>';
            echo '<div class="p-price">'.$currency.$obj->price.'</div>';
            echo '<div class="product-info">';
            echo '<h3>'.$obj->product_name.' (Code :'.$product_code.')</h3> ';
            echo '<div class="p-qty">Qty : '.$cart_itm["qty"].'</div>';
            echo '<div>'.$obj->product_desc.'</div>';
            echo '</div>';
            echo '</li>';
            $subtotal = ($cart_itm["price"]*$cart_itm["qty"]);
            $total = ($total + $subtotal);

            echo '<input type="hidden" name="item_name['.$cart_items.']" value="'.$obj->product_name.'" />';
            echo '<input type="hidden" name="item_code['.$cart_items.']" value="'.$product_code.'" />';
            echo '<input type="hidden" name="item_desc['.$cart_items.']" value="'.$obj->product_desc.'" />';
            echo '<input type="hidden" name="item_qty['.$cart_items.']" value="'.$cart_itm["qty"].'" />';
            $cart_items ++;

        }
        echo '</ul>';
        echo '<span class="check-out-txt">';
        echo '<strong>Total : '.$currency.$total.'</strong>  ';
        echo '</span>';
        echo '</form>';
    }else{
        echo 'Your Cart is empty';
    }   ';

Thank you for any help. All help is greatly appreciated.

Was it helpful?

Solution

use ob_get_contents instead to get your $body variable set properly

http://us3.php.net/ob_get_contents

This way you can write your HTML/PHP/Javascript code more naturally and not in some big string that will cause tons of debugging headaches. Then you can echo or use the output in a single line of code at the end much more elegantly.

from the php page I linked above, here is an example of it's usage:

<?php

ob_start();

echo "Hello ";

$out1 = ob_get_contents();

echo "World";

$out2 = ob_get_contents();

ob_end_clean();

var_dump($out1, $out2);
?>

OTHER TIPS

try this (not tested).

if(isset($_SESSION["products"]))
 {   
    $total = 0;
    $body = '<form method="post" action="PAYMENT-GATEWAY">';
    $body.= '<ul>';
    $cart_items = count($_SESSION["products"]);

    foreach ($_SESSION["products"] as $cart_itm)
    {
       $product_code = $cart_itm["code"];
       $queryy = "SELECT TOP 1 product_name,product_desc, price FROM products WHERE product_code='".$product_code."'";
       $results = mssql_query($queryy, $mysqli);
       $obj = mssql_fetch_object($results);

        $body. = '<li class="cart-itm">';
        $body. = '<span class="remove-itm"><a href="cart_update.php?removep='.$cart_itm["code"].'&return_url='.$current_url.'">&times;</a></span>';
        $body. = '<div class="p-price">'.$currency.$obj->price.'</div>';
        $body. = '<div class="product-info">';
        $body. = '<h3>'.$obj->product_name.' (Code :'.$product_code.')</h3> ';
        $body. = '<div class="p-qty">Qty : '.$cart_itm["qty"].'</div>';
        $body. = '<div>'.$obj->product_desc.'</div>';
        $body. = '</div>';
        $body. = '</li>';
        $subtotal = ($cart_itm["price"]*$cart_itm["qty"]);
        $total = ($total + $subtotal);

        $body. = '<input type="hidden" name="item_name['.$cart_items.']" value="'.$obj->product_name.'" />';
        $body. = '<input type="hidden" name="item_code['.$cart_items.']" value="'.$product_code.'" />';
        $body. = '<input type="hidden" name="item_desc['.$cart_items.']" value="'.$obj->product_desc.'" />';
        $body. = '<input type="hidden" name="item_qty['.$cart_items.']" value="'.$cart_itm["qty"].'" />';
    }
    $body.  = 'Cart Items: '.$cart_items;
    $body.  = '</ul>';
    $body.  = '<span class="check-out-txt">';
    $body.  = '<strong>Total : '.$currency.$total.'</strong>  ';
    $body.  = '</span>';
    $body.  = '</form>';

}else{
    $body = 'Your Cart is empty';
}   

mail($to, $subject, $body, $headers);

first you need to add to the end of your string. then you will need to run eval ( $body ). However, I think you will have better luck putting your output into the body variable with concat $body = "first line"; $body .= "second line"; this will allow you to run php code to determine what you would like to add to your body rather than trying to run php code in the body string.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top