List shopping cart in phpmailer - php

I try to send an email with phpmailer after "Place Order".
The problem is that I cannot list the shopping cart as an email (product, quantity, price of each product and total price).
echo var_dump($_SESSION['cart']);
echo var_dump($_SESSION['qty_array']);
This shows me that everything seems to work.
cart.php:
<tr>
<?php
$total = 0;
if(!empty($_SESSION['cart'])){
include 'config.php';
$index = 0;
if(!isset($_SESSION['qty_array'])){
$_SESSION['qty_array'] = array_fill(0, count($_SESSION['cart']), 1);
}
$sql = "SELECT * FROM products WHERE id IN (".implode(',',$_SESSION['cart']).")";
$query = $conn->query($sql);
while($row = $query->fetch_assoc()){
?>
</tr>
<tr>
<td>
<img src="<?= $row['photo'] ?>" width="150px"><br />
<?= $row['name'] ?>
</td>
<input type="hidden" name="indexes[]" value="<?php echo $index; ?>">
<td>
<?php echo $_SESSION['qty_array'][$index]; ?>
</td>
<td>
<b><i class="fas fa-dollar-sign"></i> <?php echo number_format($_SESSION['qty_array'][$index]*$row['price'], 2); ?></b>
</td>
<?php $total += $_SESSION['qty_array'][$index]*$row['price']; ?>
</tr>
<?php
$index ++;
}
}
?>
phpmailer.php:
echo var_dump($_SESSION['cart']);
echo var_dump($_SESSION['qty_array']);
foreach($_SESSION['cart'] as $key => $product) {
$name = $product['name'];
$price = $product['price'];
$qty = $product['qty'];
$tprice = $product['totalPrice'];
}
$mail->Body = nl2br("$name\r\n$\r\n$qty\r\n$tprice");
This is not working at all. I tried a few things, and most of what worked was just the listing of the last product on the shopping cart list. But only the ID.
Edit:
I have tried another way, but still only the last item is listed.
phpmailer.php:
$total = 0;
if(!empty($_SESSION['cart'])){
include 'config.php';
$index = 0;
if(!isset($_SESSION['qty_array'])){
$_SESSION['qty_array'] = array_fill(0, count($_SESSION['cart']), 1);
}
$sql = "SELECT * FROM products WHERE id IN (".implode(',',$_SESSION['cart']).")";
$query = $conn->query($sql);
while($row = $query->fetch_assoc()){
$service = $row['name'];
$qty = $_SESSION['qty_array'][$index];
$qtyPrice = number_format($row['price'], 2);
$qtyTotalprice = number_format($_SESSION['qty_array'][$index]*$row['price'], 2);
$total += $_SESSION['qty_array'][$index]*$row['price'];
$mail->Body = nl2br("$service: ($qty) x ($$qtyPrice) = $$qtyTotalprice \r\n \r\nTOTAL: $ <u>$total</u>");
$index ++;
}
}

I found a solution:
phpmailer.php
...
$mail->Body = nl2br("Hi {$_POST['name']} \r\n");
/// List Cart Item(s) Start
$total = 0;
if(!empty($_SESSION['cart'])){
include 'config.php';
$index = 0;
if(!isset($_SESSION['cart'])){
$_SESSION['cart'] = array_fill(0, count($_SESSION['cart']), 1);
}
$sql = "SELECT * FROM products WHERE id IN (".implode(',',$_SESSION['cart']).")";
$query = $conn->query($sql);
while($row = $query->fetch_assoc()){
$index;
$service = $row['name'];
$qty = $_SESSION['cart'][$index];
$qtyPrice = $row['price'];
$qtyTotalprice = number_format($_SESSION['cart'][$index]*$row['price'], 2);
$total += $_SESSION['cart'][$index]*$row['price'];
$mail->Body .= nl2br("$service: ($qty) x ($$qtyPrice) = $$qtyTotalprice \r\n");
$index ++;
}
}
$mail->Body .= nl2br(" \r\nTOTAL: $ <u>$total</u>");
/// Cart Item(s) End
$mail->Body .= nl2br("Kind regards \r\n");
...

Related

Shopping Cart no values

https://kopy.io/6Ud9J https://kopy.io/7tFRb
In first code I have view_cart.php but it doesn't show me the values.
Viewcart.php
<form method="post" action="cart_update.php">
<table width="100%" cellpadding="6" cellspacing="0"><thead><tr><th>Quantity</th><th>Name</th><th>pret</th><th>Total</th><th>Remove</th></tr></thead>
<tbody>
<?php
if(isset($_SESSION['prodcos'])) //check session var
{
$total = 0; //set initial total value
$b = 0; //var for zebra stripe table
foreach ($_SESSION['prodcos'] as $cart_itm)
{
var_dump($cart_itm);
//set variables to use in content below
$titlu = $cart_itm['titlu'];
$cantitate = $cart_itm['cantitate'];
$pret = $cart_itm['pret'];
$id = $cart_itm['id'];
$subtotal = ($pret * $cantitate); //calculate pret x Qty
var_dump($titlu);
var_dump($pret);
var_dump($titlu);
echo '<tr>';
echo '<td><input type="text" size="2" maxlength="2" name="cantitate['.$id.']" value="'.$cantitate.'" /></td>';
echo '<td>'.$titlu.'</td>';
echo '<td>'.$pret.$currency.'</td>';
echo '<td>'.$currency.$subtotal.'</td>';
echo '<td><input type="checkbox" name="remove_code[]" value="'.$id.'" /></td>';
echo '</tr>';
$total = ($total + $subtotal); //add subtotal to total var
}
$grand_total = $total + $shipping_cost; //grand total including shipping cost
foreach($taxes as $key => $value){ //list and calculate all taxes in array
$tax_amount = round($total * ($value / 100));
$tax_item[$key] = $tax_amount;
$grand_total = $grand_total + $tax_amount; //add tax val to grand total
}
$list_tax = '';
foreach($tax_item as $key => $value){ //List all taxes
$list_tax .= $key. ' : '. $currency. sprintf("%01.2f", $value).'<br />';
}
$shipping_cost = ($shipping_cost)?'Shipping Cost : '.$currency. sprintf("%01.2f", $shipping_cost).'<br />':'';
}
?>
<tr><td colspan="5"><span style="float:right;text-align: right;"><?php echo $shipping_cost. $list_tax; ?>Amount Payable : <?php echo sprintf("%01.2f", $grand_total);?></span></td></tr>
<tr><td colspan="5">Add More Items<button type="submit">Update</button></td></tr>
</tbody>
</table>
<input type="hidden" name="return_url" value="<?php
$current_url = urlencode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
echo $current_url; ?>" />
</form>
update_cart.php
<?php
session_start();
include_once("includes/config.php");
//add product to session or create new one
if(isset($_POST['type']) && $_POST['type']=='add' && $_POST['cantitate']>0)
{
foreach($_POST as $key => $value){ //add all post vars to new_product array
$new_product[$key] = filter_var($value, FILTER_SANITIZE_STRING);
}
//remove unecessary vars
unset($new_product['type']);
unset($new_product['return_url']);
$id = $_POST['id'];
$sth = $db->prepare("SELECT titlu, pret FROM produse WHERE id = :id");
$sth->bindParam(':id', $id);
$sth->execute();
// $statement = $db->query('SELECT titlu, pret FROM produse WHERE id="$id"');
while($sth->fetch(PDO::FETCH_ASSOC)){
//fetch product name, pret from db and add to new_product array
$new_product['titlu'] = $titlu;
$new_product['pret'] = $pret;
if(isset($_SESSION['prodcos'])){ //if session var already exist
if(isset($_SESSION['prodcos'][$new_product['id']])) //check item exist in products array
{
unset($_SESSION['prodcos'][$new_product['id']]); //unset old array item
}
}
$_SESSION['prodcos'][$new_product['id']] = $new_product; //update or create product session with new item
}
}
//update or remove items
if(isset($_POST['cantitate']) || isset($_POST['remove_code']))
{
//update item quantity in product session
if(isset($_POST['cantitate']) && is_array($_POST['cantitate'])){
foreach($_POST['cantitate'] as $key => $value){
if(is_numeric($value)){
$_SESSION['prodcos'][$key]['cantitate'] = $value;
}
}
}
//remove an item from product session
if(isset($_POST['remove_code']) && is_array($_POST['remove_code'])){
foreach($_POST['remove_code'] as $key){
unset($_SESSION['prodcos'][$key]);
}
}
}
//back to return url
$return_url = (isset($_POST['return_url']))?urldecode($_POST['return_url']):''; //return url
header('Location:'.$return_url);
?>
When I ADD the product, in shopping cart, it doesn't show the details..

PHP MySQL Dual While Loop giving me problems

I currently have a piece of code that I want to essentially pulls products quantity and prices and then give a grand total
The problem I have is the mathematics is way off... it's essentially multiplying the last quantity with the last price of the item during the loop (I hope I explained that with atleast half an ounce of clarity haha!)
Anyways here is the code, I think it was along the correct lines but somewhere I have gone wrong, Thanks!
<table>
<tr>
<th>Product Title</th>
<th>Nicotine Strength</th>
<th>Quantity</th>
<th>Price (inc. VAT)</th>
</tr>
<?php
$query = "SELECT `product`, `variant`, `quantity` FROM orders_detail WHERE order_id = '$orderid'";
$result = mysqli_query($con, $query);
$quantitytotal = 0;
while ($row = mysqli_fetch_assoc($result)) {
$product = $row['product'];
$stuff = $row['quantity'];
$variant = $row['variant'];
$linequantity = $stuff;
$quantitytotal += $stuff;
$pricequery = "SELECT product_price FROM products WHERE product_name = '$product'";
$priceresult = mysqli_query($con, $pricequery);
$pricetag = 0;
$priceline = 0;
while ($rowprice = mysqli_fetch_assoc($priceresult)) {
$price = $rowprice['product_price'];
$priceline = $price;
$pricetag += $price;
}
echo "Price: $pricetag<br>";
echo "Quantity: $quantitytotal<br>";
$linetotal = $priceline * $linequantity;
//echo "$product - $linequantity - $linetotal<br>";
echo '<tr><td>' . $product .' </td> ' . '<td>' . $variant . '</td>' . ' <td> ' . $linequantity . '</td>' . '<td> £' . $linetotal . '</td> </tr>';
}
$total = $pricetag * $quantitytotal;
?>
<tr><td>Total Ex Vat:</td><td> Total Inc Vat:</td></tr>
<tr><td><?php echo "£" . ($total / 1.2);?></td>
<td><?php echo "£" . $total; ?></td></tr>
</table>
You can calculate grand total by adding $linetotal in the loop.
$total=$total+$linetotal;
Initialize $total with 0 before loop.

For each loop not looping all rows

I'm trying to loop all rows of a table name cart but the foreach is only displaying the last row entered and always ignore the previous ones. Say I have 5 products in the cart_table well only product_id[5] will be displayed. If user adds a sixth item, only product_id[6] will now be displayed. The $item_count will also always equal 1 as if there is only one item when there are multiple. To my understanding in the foreach($items as $item) $items is not seen as an array even with multiple items. when I var_dump($items); it shows array(1) { [0]=>...
add_cart.php
<?php
ob_start();
require_once $_SERVER['DOCUMENT_ROOT'].'/ecommerce/core/init.php';
$product_id = isset($_POST['product_id'])? sanitize($_POST['product_id']):'';
$size = isset($_POST['size'])? sanitize($_POST['size']):'';
$available = isset($_POST['available'])? sanitize($_POST['available']):'';
$quantity = isset($_POST['quantity'])? sanitize($_POST['quantity']):'';
$item = array();
$item[] = array(
'id' => $product_id,
'size' => $size,
'quantity' => $quantity,
'available' => $available
);
$domain =($_SERVER['HTTP_HOST'] != 'localhost')?'.'.$_SERVER['HTTP_HOST']:false;
$query = $db->query("SELECT * FROM product WHERE id = '{$product_id}'");
$product = mysqli_fetch_assoc($query);
$_SESSION['success_flash'] = $product['prod_name']. ' was added to your cart.';
//check if the cart cookie exists
if (is_array($cart_id != ' ')) {
$cartQ = $db->query("SELECT * FROM cart WHERE id = '{$cart_id}'");
$cart = mysqli_fetch_assoc($cartQ);
$previous_items = json_decode($cart['items'],true);
$item_match = 0;
$new_items = array();
foreach($previous_items as $pitem){
if ($item[0]['id'] == $pitem['id'] && $item[0]['size'] == $pitem['size']) {
$pitem['quantity'] = $pitem['quantity'] + $item[0]['quantity'];
if ($pitem['quantity'] > $available) {
$pitem['quantity'] = $available;
}
$item_match = 1;
}
$new_items[] = $pitem;
}
if ($item_match != 1) {
$new_items = array_merge($item,$previous_items);
}
$items_json = json_encode($new_items);
$cart_expire = date("Y-m-d H:i:s",strtotime("+30 days"));
$db->query("UPDATE cart SET items = '{$items_json}', expire_date = '{$cart_expire}' WHERE id = '{cart_id}'");
setcookie(CART_COOKIE,'',1,"/",$domain,false);
setcookie(CART_COOKIE,$cart_id,CART_COOKIE_EXPIRE,'/',$domain,false);
}else{
// add to databse and set cookie
$items_json = json_encode($item);
$cart_expire = date("Y-m-d H:i:s",strtotime("+30 days"));
$db->query("INSERT INTO cart (items,expire_date) VALUES ('{$items_json}','{$cart_expire}')");
$cart_id = $db->insert_id;
setcookie(CART_COOKIE,$cart_id,CART_COOKIE_EXPIRE,'/',$domain,false);
}
?>
cart.php
<?php
require_once 'core/init.php';
//include 'includes/headerpartial.php';
if($cart_id != ' ') {
$cartQ = $db->query("SELECT * FROM cart WHERE id ='{$cart_id}' ");
$result = mysqli_fetch_assoc($cartQ);
$items = json_decode($result['items'], true);
$i = 1;
$sub_total = 0;
$item_count = 0;
}
?>
<?php if($cart_id == ' '): ?>
<div class="bg-danger">
<p class='text-center text-danger'>Your cart is empty.</p>
</div>
<?php else: ?>
<?php
foreach ($items as $item) {
var_export($items);
$product_id = $item['id'];
$productQuery = $db->query("SELECT * FROM product WHERE id ='{$product_id}' ");
$product = mysqli_fetch_assoc($productQuery);
$sArray = explode(',', $product['sizes']);
/* foreach ($sArray as $sizeString) {
$s = explode(':', $sizeString);
if($s[0] == $item['size']) {
$available = $s[1];
}
}*/
?>
<tr class="p">
<td class="image"><img src="<?= $product['image_1']; ?>"/></td>
<td class="name"><?= $product['prod_name']; ?></td>
<td class="price"><?= money($product['price']); ?></td>
<td class="quantity"><?= $item['quantity']; ?></td>
<td class="pricesubtotal"><?= money($item['quantity'] * $product['price']); ?></td>
<td class="remove">
<div>&times</div>
</td>
</tr>
<?php
$i ++;
$item_count += $item['quantity'];
$sub_total += ($product['price'] * $item['quantity']);
}
$tax = TAXRATE * $sub_total;
$tax = number_format($tax, 2);
$grand_total = $tax + $sub_total;
<?php endif;?>
Here is one of the problems. You told me that your id is an auto increment int, so I would like to propose this answer. The problem is in your sql command.
UPDATE cart SET items = '{$items_json}', expire_date = '{$cart_expire}' WHERE id = {cart_id}
Be sure to put that command in quotes. Also, I highly recommend preparing the JSON with this command:
$itemJson = addslashes($itemJson);
then, you can run the command. Another possibility would be to use the prepare method from mysqli. Here is a link to some examples:
w3schools.com
if you have any further questions, feel free to update your question, but be sure to #McStuffins in the comments.

Mysql row returns last value in array, but I expect the whole row

Here is my cart code:
function cart() {
$total = 0;
$item_quantity = 0;
$item_name = 0;
$item_number = 1;
$amount = 1;
$quantity = 1;
foreach ($_SESSION as $name => $value) {
if($value > 0 ) {
if(substr($name, 0, 8) == "product_"){
$length = strlen($name - 8);
$id = substr($name, 8 , $length);
$query = query("SELECT * FROM products WHERE product_id = " . escape_string($id)." ");
confirm($query);
$titleArr = array();
while($row = fetch_array($query)) {
$titleArr[$id] = $row['product_title'];
$product_name = implode(",", $titleArr);
$order_product = $product_name . "-". $value;
$sub = $row['product_price']*$value;
$item_quantity +=$value;
$item_number++;
$amount++;
$quantity++;
$id++;
}
$_SESSION['item_total'] = $total += $sub;
$_SESSION['item_quantity'] = $item_quantity;
$_SESSION['item_name'] = $order_product;
}
}
}
}
I searched online for a long time, please advise.
$_SESSION['item_name'] = $order_product;
It gives last value from fetch_array I want all product title on cart page.
Either of these changes should work...
This puts the m all into an array...
$_SESSION['item_name'] = array();
foreach ($_SESSION as $name => $value) {
...
$_SESSION['item_total'] = $total += $sub;
$_SESSION['item_quantity'] = $item_quantity;
array_push($_SESSION['item_name'],$order_product);
...
}
This puts them into a comma separated string...
$_SESSION['item_name'] = '';
foreach ($_SESSION as $name => $value) {
...
$_SESSION['item_total'] = $total += $sub;
$_SESSION['item_quantity'] = $item_quantity;
$_SESSION['item_name'] .= $order_product.', ';
...
}
$_SESSION['item_name'] = rtrim($_SESSION['item_name'],' ');
$_SESSION['item_name'] = rtrim($_SESSION['item_name'],',');
Please make two dimensional Array, if you want to show all product title on cart page.
Replace this
$order_product = $product_name . "-". $value;
with
$order_product[] = $product_name . "-". $value;
Before while loop assign the array:-
$order_product = array();
updated while loop
$total = 0
while($row = fetch_array($query)) {
$titleArr[$id] = $row['product_title'];
$product_name = implode(",", $titleArr);
$order_product = $product_name . "-". $value;
$sub = $row['product_price']*$value;
$item_quantity +=$value;
$item_number++;
$amount++;
$quantity++;
$id++;
$total += $sub;
$_SESSION['item_name'][] = $order_product;
}
$_SESSION['item_total'] = $total;
$_SESSION['item_quantity'] = $item_quantity;

How to get total during a foreach

I am learning PHP, and trying to output the total of the "Price" column.
Here's what it looks like right now:
http://imgur.com/X00o4jS
Here's the code:
while($row = $result->fetch_array()){
$rows[] = $row;
}
foreach($rows as $row){
$food = $row["food"];
$price = $row["price"];
$id = $row['id'];
if(!empty($_POST[$id])){
$qtyPrice = $price * $_POST[$id];
$qty = $_POST[$id];
}
echo "<tr>
<td>$food</td>
<td>$qty</td>
<td>$$qtyPrice</td>
</tr>";
}
No need to do 2 loops. You are setting the array with the 1st while loop and then using a 2nd foreach loop just to read it and display the data.
It will be more efficient to use the while loop to generate your html table directly.
You can use $total += $qtyPrice (same as $total = $total + $qtyPrice).
Just remember to set it to 0 before the loop.
$total = 0;
while($row = $result->fetch_array()){
$food = $row["food"];
$price = $row["price"];
$id = $row['id'];
if(!empty($_POST[$id])){
$qtyPrice = $price * $_POST[$id];
$qty = $_POST[$id];
$total += $qtyPrice;
echo "<tr>
<td>$food</td>
<td>$qty</td>
<td>$$qtyPrice</td>
</tr>";
}
//display the total in its separate row
echo "<tr><td>Total:</td><td></td><td>$total</td></tr>";
Hope this helps!
set a variable before starting the loop, and adding the qtyPrice at it. Print that variable after exit the loop
$total = 0;
foreach($rows as $row){
$food = $row["food"];
$price = $row["price"];
$id = $row['id'];
if(!empty($_POST[$id])){
$qtyPrice = $price * $_POST[$id];
$total += $qtyPrice;
$qty = $_POST[$id];
}
echo "<tr>
<td>$food</td>
<td>$qty</td>
<td>$$qtyPrice</td>
</tr>";
}
echo $total;

Categories