Bonsoir, j'ai une session d'un panier
$_SESSION["cart_products"]
Je voudrais récuperer quelques informations j'ai donc fais un var dump de cette session et je suis tombé sur ca
1
array(2) { ["PD1002"]=> array(7) { ["product_color"]=> string(5) "Black" ["product_qty"]=> string(1) "1" ["product_code"]=> string(6) "PD1002" ["product_name"]=> string(14) "Television DXT" ["product_price"]=> string(6) "100.85" ["product_img_name"]=> string(10) "lcd-tv.jpg" ["product_desc"]=> string(71) "NIKE, Innovation pure des chaussures purement personalisé à vos gouts" } ["PD1003"]=> array(7) { ["product_color"]=> string(5) "Black" ["product_qty"]=> string(1) "1" ["product_code"]=> string(6) "PD1003" ["product_name"]=> string(18) "External Hard Disk" ["product_price"]=> string(6) "100.00" ["product_img_name"]=> string(22) "external-hard-disk.jpg" ["product_desc"]=> string(71) "NIKE, Innovation pure des chaussures purement personalisé à vos gouts" }
Ce que je voudrais c'est de récuperer les product name de mon panier et les stocker dans une variable.
Voici mes deux pages: la premiere pour afficher mon panier:
<?php
session_start();
include_once("config.php");
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400&display=swap" rel="stylesheet">
<title>View shopping cart</title>
<link href="style/panier.css" rel="stylesheet" type="text/css"></head>
<body>
<h1 align="center">View Cart</h1>
<form method="post" action="cart_update.php">
<div id="container">
<h2 class="panier">MON PANIER</h2>
<tbody>
<?php
if(isset($_SESSION["cart_products"])) //check session var
{
$total = 0; //set initial total value
$b = 0; //var for zebra stripe table
$ad = $_SESSION['cart_products'];
foreach ($_SESSION["cart_products"] as $cart_itm)
{
//set variables to use in content below
$product_img_name = $cart_itm["product_img_name"];
$product_name = $cart_itm["product_name"];
$product_qty = $cart_itm["product_qty"];
$product_price = $cart_itm["product_price"];
$product_code = $cart_itm["product_code"];
$product_desc = $cart_itm["product_desc"];
$subtotal = ($product_price * $product_qty); //calculate Price x Qty
echo '<div style="" class="article">';
echo '<div class="separator"></div>';
echo '<img style="float:left; width:30%; padding-right: 8%;" src="images/' . $product_img_name . '"/>';
echo '<div style="float:left; width: 60%;" class="right">';
echo '<button type="submit" class="remove" name="remove_code[]" value="'.$product_code.'" /></button>';
echo '<h4 style="height: 25px;">'.$product_name.'<br>';
echo '' .$currency.$product_price.'</h4>';
echo '<p style="">'.$product_desc.'';
echo '<br>';
echo '<input style="margin-top:5px;" type="text" size="2" maxlength="2" name="product_qty['.$product_code.']" value="'.$product_qty.'" />';
echo '</div>';
echo '<br> <br><br><br>';
echo '</div>';
$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
$grand_total_stripe = $grand_total * 100;
}
$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 />':'';
}
else{
header('Location: http://www.votresite.com/pageprotegee.php');
}
?>
</form>
<div class="payement">
<div class="container_payement">
<form method="post" action="final.php">
<?php
echo $product_name;
?>
<?php
$_SESSION['grand_total_stripe'] = $grand_total_stripe;
?>
<span>Prenom* :</span><br>
<input class="formulaire" style="" type="text" name="prenom">
<span>Nom* :</span><br>
<input class="formulaire" style="" type="text" name="nom">
<span>Pays* :</span><br>
<select style="width: 101%;" class="formulaire" name="pays" id="pet-select">
<option value="">France</option>
<option value="dog">Dog</option>
<option value="cat">Cat</option>
<option value="hamster">Hamster</option>
<option value="parrot">Parrot</option>
<option value="spider">Spider</option>
<option value="goldfish">Goldfish</option>
</select>
<span>Code postal* :</span><br>
<input class="formulaire" style="" type="text" name="postal">
<span>Ville* :</span><br>
<input class="formulaire" style="" type="text" name="ville">
<span>Adresse* :</span><br>
<input class="formulaire" style="" type="text" name="adresse">
<?php echo $shipping_cost. $list_tax; ?>Amount Payable : <?php echo sprintf("%01.2f", $grand_total);?> <br>
<input class="submit" type="submit" name="bouton" value="Envoyer">
</form>
</div>
</div>
<input type="hidden" name="return_url" value="<?php
$current_url = urlencode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
echo $current_url; ?>" />
</body>
</html>
Et l'autre page php pour l'ajout / update :
<?php
session_start();
include_once("config.php");
//add product to session or create new one
if(isset($_POST["type"]) && $_POST["type"]=='add' && $_POST["product_qty"]>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']);
//we need to get product name and price from database.
$statement = $mysqli->prepare("SELECT product_name, price, product_img_name, product_desc FROM products WHERE product_code=? LIMIT 1");
$statement->bind_param('s', $new_product['product_code']);
$statement->execute();
$statement->bind_result($product_name, $price, $product_img_name, $product_desc);
while($statement->fetch()){
//fetch product name, price from db and add to new_product array
$new_product["product_name"] = $product_name;
$new_product["product_price"] = $price;
$new_product["product_img_name"] = $product_img_name;
$new_product["product_desc"] = $product_desc;
if(isset($_SESSION["cart_products"])){ //if session var already exist
if(isset($_SESSION["cart_products"][$new_product['product_code']])) //check item exist in products array
{
unset($_SESSION["cart_products"][$new_product['product_code']]); //unset old array item
}
}
$_SESSION["cart_products"][$new_product['product_code']] = $new_product; //update or create product session with new item
}
}
//update or remove items
if(isset($_POST["product_qty"]) || isset($_POST["remove_code"]))
{
//update item quantity in product session
if(isset($_POST["product_qty"]) && is_array($_POST["product_qty"])){
foreach($_POST["product_qty"] as $key => $value){
if(is_numeric($value)){
$_SESSION["cart_products"][$key]["product_qty"] = $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["cart_products"][$key]);
}
}
}
//back to return url
$return_url = (isset($_POST["return_url"]))?urldecode($_POST["return_url"]):''; //return url
header('Location:'.$return_url);
?>
J'arrive à afficher le $product_name mais malheusement sa récupere que le dernier article ajouté, je voudrais les avoir tous pour les stocker dans ma base de donné après le paiement
J'ai reussis à tous les recuperer avec un foreach mais pour les integrer dans ma base de donné, sa créer une ligne pour chaque article.
J'éspere bien m'etre exprimé merci d'avance :D