I have this shopping cart in which i store the data of selected products as arrays in session.
This is what the arrays look like in the session:
Array
(
[4] => Array
(
[name] => Елемвитал с органичен йод
[id] => 4
[quantity] => 1
[price] => 16.44
)
[3] => Array
(
[name] => Ритми на здраве
[id] => 3
[quantity] => 3
[price] => 18.99
)
)
Now I have a file cart.php in which i list all the products with foreach. Everything works as it should. Then i have a form below the foreach which takes user input name,address etc, info for the checkout. I use bootstrap form validation plugin which takes the user input and sends it to a php file through jquery ajax and then mails the information to my email. I want to somehow include the information taken out of the session too. I tried to just put a class on a div in the foreach loop and it lists the products like so: productproductproduct. Which is very hard to read. Then i tried with json_encode in jquery and i get this result:
{"4":{"name":"Елемвитал с органичен йод","id":"4","quantity":"1","price":"16.44"},"3":{"name":"Ритми на здраве","id":"3","quantity":"3","price":"18.99"}}
Which is again very hard to read if i mail it like this. Is there a way to take out the name and the quantity of each array in the session and list it like so: Items: ([name][quantity],[name][quantity]? Please someone help me.
Well, when you send your mail, you could add the product info like this :
<?php
$email_body = "A new order was made.
";
$strProducts = '';
if (isset($_SESSION['products']) && is_array($_SESSION['products'])) {
foreach($_SESSION['produicts'] as $product) {
$strProducts .= ($strProducts != '' ? ', ' : '(') . $product['name'] . ' ' . $product['quantity'];
}
$strProducts .= ")";
}
$email_body .= $strProducts;
Assuming your session variable name is "products"
You can create a table
for each name
and quantity
like,
<?php
$json = $yourJsonObj;
$table='<table><tr><th>Name</th><th>Quantity</th><th>Price</th></tr>';
foreach($json as $key=>$value){
$table.='<tr><td>'.$value['name'].'</td><td>'.$value['quantity'].'</td><td>'.$value['price'].'</td></tr>';
}
$table.='</table>';
// now mail this table in your mail body
?>