I am playing around with a PHP shopping cart and I am trying to send the order of the customer via email to the seller.
My problem is that if there are multiple items ordered, only the last one is actually sent in the email but when I echo, all the items are displayed.
Could you please explain why this is happening?
if(isset($_SESSION["products"])){
$total = 0;
$cart_items = 0;
foreach ($_SESSION["products"] as $cart_itm){
$product_code = $cart_itm["code"];
$results = $mysqli->query("SELECT product_name,product_desc, price FROM products WHERE product_code='$product_code' LIMIT 1");
$rows = array();
while ($obj = $results->fetch_object()){
$rows[] = $obj;
echo $rows[0]->product_name.' x '.$cart_itm["qty"].' ; '; // here is ok
$prod_name = ($rows[0]->product_name); // here only the last product displays. Why?!
}
$subtotal = ($cart_itm["price"]*$cart_itm["qty"]);
$total = ($total + $subtotal);
$cart_items ++;
}
}
$prod_name = str_replace($cyr, $lat, $prod_name);
$random = rand(72891, 92729);
$subject = "New order #$random";
$message = "You have new order from $name $lname with adress $curraddr and order details: $prod_name with a total value of $total dollars.";
$from = "From: admin@mail.xyz";
$to = "mail@mail.com";
mail($to, $subject, $message, $from);